home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / benchmarks / itc / cc1.spur / cse.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-30  |  95.0 KB  |  3,363 lines

  1. /* Common subexpression elimination for GNU compiler.
  2.    Copyright (C) 1987, 1988 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY.  No author or distributor
  8. accepts responsibility to anyone for the consequences of using it
  9. or for whether it serves any particular purpose or works at all,
  10. unless he says so in writing.  Refer to the GNU CC General Public
  11. License for full details.
  12.  
  13. Everyone is granted permission to copy, modify and redistribute
  14. GNU CC, but only under the conditions described in the
  15. GNU CC General Public License.   A copy of this license is
  16. supposed to have been given to you along with GNU CC so you
  17. can know your rights and responsibilities.  It should be in a
  18. file named COPYING.  Among other things, the copyright notice
  19. and this notice must be preserved on all copies.  */
  20.  
  21.  
  22. #include "config.h"
  23. #include "rtl.h"
  24. #include "insn-config.h"
  25. #include "regs.h"
  26. #include "hard-reg-set.h"
  27. #include "alloca.h"
  28.  
  29. /* The basic idea of common subexpression elimination is to go
  30.    through the code, keeping a record of expressions that would
  31.    have the same value at the current scan point, and replacing
  32.    expressions encountered with the cheapest equivalent expression.
  33.  
  34.    It is too complicated to keep track of the different possibilities
  35.    when control paths merge; so, at each label, we forget all that is
  36.    known and start fresh.  This can be described as processing each
  37.    basic block separately.  Note, however, that these are not quite
  38.    the same as the basic blocks found by a later pass and used for
  39.    data flow analysis and register packing.  We do not need to start fresh
  40.    after a conditional jump instruction if there is no label there.
  41.  
  42.    We use two data structures to record the equivalent expressions:
  43.    a hash table for most expressions, and several vectors together
  44.    with "quantity numbers" to record equivalent (pseudo) registers.
  45.  
  46.    The use of the special data structure for registers is desirable
  47.    because it is faster.  It is possible because registers references
  48.    contain a fairly small number, the register number, taken from
  49.    a contiguously allocated series, and two register references are
  50.    identical if they have the same number.  General expressions
  51.    do not have any such thing, so the only way to retrieve the
  52.    information recorded on an expression other than a register
  53.    is to keep it in a hash table.
  54.  
  55. Registers and "quantity numbers":
  56.    
  57.    At the start of each basic block, all of the (hardware and pseudo)
  58.    registers used in the function are given distinct quantity
  59.    numbers to indicate their contents.  During scan, when the code
  60.    copies one register into another, we copy the quantity number.
  61.    When a register is loaded in any other way, we allocate a new
  62.    quantity number to describe the value generated by this operation.
  63.    `reg_qty' records what quantity a register is currently thought
  64.    of as containing.
  65.  
  66.    We also maintain a bidirectional chain of registers for each
  67.    quantity number.  `qty_first_reg', `qty_last_reg',
  68.    `reg_next_eqv' and `reg_prev_eqv' hold these chains.
  69.  
  70.    The first register in a chain is the one whose lifespan is least local.
  71.    Among equals, it is the one that was seen first.
  72.    We replace any equivalent register with that one.
  73.  
  74. Constants and quantity numbers
  75.  
  76.    When a quantity has a known constant value, that value is stored
  77.    in the appropriate element of qty_const.  This is in addition to
  78.    putting the constant in the hash table as is usual for non-regs.
  79.  
  80.    Regs are preferred to constants as they are to everything else,
  81.    but expressions containing constants can be simplified, by fold_rtx.
  82.  
  83.    When a quantity has a known nearly constant value (such as an address
  84.    of a stack slot), that value is stored in the appropriate element
  85.    of qty_const.
  86.  
  87.    Integer constants don't have a machine mode.  However, cse
  88.    determines the intended machine mode from the destination
  89.    of the instruction that moves the constant.  The machine mode
  90.    is recorded in the hash table along with the actual RTL
  91.    constant expression so that different modes are kept separate.
  92.  
  93. Other expressions:
  94.  
  95.    To record known equivalences among expressions in general
  96.    we use a hash table called `table'.  It has a fixed number of buckets
  97.    that contain chains of `struct table_elt' elements for expressions.
  98.    These chains connect the elements whose expressions have the same
  99.    hash codes.
  100.  
  101.    Other chains through the same elements connect the elements which
  102.    currently have equivalent values.
  103.  
  104.    Register references in an expression are canonicalized before hashing
  105.    the expression.  This is done using `reg_qty' and `qty_first_reg'.
  106.    The hash code of a register reference is computed using the quantity
  107.    number, not the register number.
  108.  
  109.    When the value of an expression changes, it is necessary to remove from the
  110.    hash table not just that expression but all expressions whose values
  111.    could be different as a result.
  112.  
  113.      1. If the value changing is in memory, except in special cases
  114.      ANYTHING referring to memory could be changed.  That is because
  115.      nobody knows where a pointer does not point.
  116.      The function `invalidate_memory' removes what is necessary.
  117.  
  118.      The special cases are when the address is constant or is
  119.      a constant plus a fixed register such as the frame pointer
  120.      or a static chain pointer.  When such addresses are stored in,
  121.      we can tell exactly which other such addresses must be invalidated
  122.      due to overlap.  `invalidate' does this.
  123.      All expressions that refer to non-constant
  124.      memory addresses are also invalidated.  `invalidate_memory' does this.
  125.  
  126.      2. If the value changing is a register, all expressions
  127.      containing references to that register, and only those,
  128.      must be removed.
  129.  
  130.    Because searching the entire hash table for expressions that contain
  131.    a register is very slow, we try to figure out when it isn't necessary.
  132.    Precisely, this is necessary only when expressions have been
  133.    entered in the hash table using this register, and then the value has
  134.    changed, and then another expression wants to be added to refer to
  135.    the register's new value.  This sequence of circumstances is rare
  136.    within any one basic block.
  137.  
  138.    The vectors `reg_tick' and `reg_in_table' are used to detect this case.
  139.    reg_tick[i] is incremented whenever a value is stored in register i.
  140.    reg_in_table[i] holds -1 if no references to register i have been
  141.    entered in the table; otherwise, it contains the value reg_tick[i] had
  142.    when the references were entered.  If we want to enter a reference
  143.    and reg_in_table[i] != reg_tick[i], we must scan and remove old references.
  144.    Until we want to enter a new entry, the mere fact that the two vectors
  145.    don't match makes the entries be ignored if anyone tries to match them.
  146.  
  147.    Registers themselves are entered in the hash table as well as in
  148.    the equivalent-register chains.  However, the vectors `reg_tick'
  149.    and `reg_in_table' do not apply to expressions which are simple
  150.    register references.  These expressions are removed from the table
  151.    immediately when they become invalid, and this can be done even if
  152.    we do not immediately search for all the expressions that refer to
  153.    the register.
  154.  
  155.    A CLOBBER rtx in an instruction invalidates its operand for further
  156.    reuse.  A CLOBBER or SET rtx whose operand is a MEM:BLK
  157.    invalidates everything that resides in memory.
  158.  
  159. Related expressions:
  160.  
  161.    Constant expressions that differ only by an additive integer
  162.    are called related.  When a constant expression is put in
  163.    the table, the related expression with no constant term
  164.    is also entered.  These are made to point at each other
  165.    so that it is possible to find out if there exists any
  166.    register equivalent to an expression related to a given expression.  */
  167.    
  168. /* One plus largest register number used in this function.  */
  169.  
  170. static int max_reg;
  171.  
  172. /* Length of vectors indexed by quantity number.
  173.    We know in advance we will not need a quantity number this big.  */
  174.  
  175. static int max_qty;
  176.  
  177. /* Next quantity number to be allocated.
  178.    This is 1 + the largest number needed so far.  */
  179.  
  180. static int next_qty;
  181.  
  182. /* Indexed by quantity number, gives the first (or last) (pseudo) register 
  183.    in the chain of registers that currently contain this quantity.  */
  184.  
  185. static int *qty_first_reg;
  186. static int *qty_last_reg;
  187.  
  188. /* Indexed by quantity number, gives the rtx of the constant value of the
  189.    quantity, or zero if it does not have a known value.
  190.    A sum of the frame pointer (or arg pointer) plus a constant
  191.    can also be entered here.  */
  192.  
  193. static rtx *qty_const;
  194.  
  195. /* Indexed by qty number, gives the insn that stored the constant value
  196.    recorded in `qty_const'.  */
  197.  
  198. static rtx *qty_const_insn;
  199.  
  200. /* Value stored in CC0 by previous insn:
  201.    0 if previous insn didn't store in CC0.
  202.    else 0100 + (M&7)<<3 + (N&7)
  203.    where M is 1, 0 or -1 if result was >, == or < as signed number
  204.    and N is 1, 0 or -1 if result was >, == or < as unsigned number.
  205.    0200 bit may also be set, meaning that only == and != comparisons
  206.    have known results.  */
  207.  
  208. static int prev_insn_cc0;
  209.  
  210. /* Previous actual insn.  0 if at first insn of basic block.  */
  211.  
  212. static rtx prev_insn;
  213.  
  214. /* Insn being scanned.  */
  215.  
  216. static rtx this_insn;
  217.  
  218. /* Index by (pseudo) register number, gives the quantity number
  219.    of the register's current contents.  */
  220.  
  221. static int *reg_qty;
  222.  
  223. /* Index by (pseudo) register number, gives the number of the next
  224.    (pseudo) register in the chain of registers sharing the same value.
  225.    Or -1 if this register is at the end of the chain.  */
  226.  
  227. static int *reg_next_eqv;
  228.  
  229. /* Index by (pseudo) register number, gives the number of the previous
  230.    (pseudo) register in the chain of registers sharing the same value.
  231.    Or -1 if this register is at the beginning of the chain.  */
  232.  
  233. static int *reg_prev_eqv;
  234.  
  235. /* Index by (pseudo) register number, gives the latest rtx
  236.    to use to insert a ref to that register.  */
  237.  
  238. static rtx *reg_rtx;
  239.  
  240. /* Index by (pseudo) register number, gives the number of times
  241.    that register has been altered in the current basic block.  */
  242.  
  243. static int *reg_tick;
  244.  
  245. /* Index by (pseudo) register number, gives the reg_tick value at which
  246.    rtx's containing this register are valid in the hash table.
  247.    If this does not equal the current reg_tick value, such expressions
  248.    existing in the hash table are invalid.
  249.    If this is -1, no expressions containing this register have been
  250.    entered in the table.  */
  251.  
  252. static int *reg_in_table;
  253.  
  254. /* Two vectors of max_reg ints:
  255.    one containing all -1's; in the other, element i contains i.
  256.    These are used to initialize various other vectors fast.  */
  257.  
  258. static int *all_minus_one;
  259. static int *consec_ints;
  260.  
  261. /* UID of insn that starts the basic block currently being cse-processed.  */
  262.  
  263. static int cse_basic_block_start;
  264.  
  265. /* UID of insn that ends the basic block currently being cse-processed.  */
  266.  
  267. static int cse_basic_block_end;
  268.  
  269. /* Nonzero if cse has altered conditional jump insns
  270.    in such a way that jump optimization should be redone.  */
  271.  
  272. static int cse_jumps_altered;
  273.  
  274. /* canon_hash stores 1 in do_not_record
  275.    if it notices a reference to CC0, CC1 or PC.  */
  276.  
  277. static int do_not_record;
  278.  
  279. /* canon_hash stores 1 in hash_arg_in_memory
  280.    if it notices a reference to memory within the expression being hashed.  */
  281.  
  282. static int hash_arg_in_memory;
  283.  
  284. /* canon_hash stores 1 in hash_arg_in_struct
  285.    if it notices a reference to memory that's part of a structure.  */
  286.  
  287. static int hash_arg_in_struct;
  288.  
  289. /* The hash table contains buckets which are chains of `struct table_elt's,
  290.    each recording one expression's information.
  291.    That expression is in the `exp' field.
  292.  
  293.    Those elements with the same hash code are chained in both directions
  294.    through the `next_same_hash' and `prev_same_hash' fields.
  295.  
  296.    Each set of expressions with equivalent values
  297.    are on a two-way chain through the `next_same_value'
  298.    and `prev_same_value' fields, and all point with
  299.    the `first_same_value' field at the first element in
  300.    that chain.  The chain is in order of increasing cost.
  301.    Each element's cost value is in its `cost' field.
  302.  
  303.    The `in_memory' field is nonzero for elements that
  304.    involve any reference to memory.  These elements are removed
  305.    whenever a write is done to an unidentified location in memory.
  306.    To be safe, we assume that a memory address is unidentified unless
  307.    the address is either a symbol constant or a constant plus
  308.    the frame pointer or argument pointer.
  309.  
  310.    The `in_struct' field is nonzero for elements that
  311.    involve any reference to memory inside a structure or array.
  312.  
  313.    The `equivalence_only' field means that this expression came from a
  314.    REG_EQUIV or REG_EQUAL note; it is not valid for substitution into an insn.
  315.  
  316.    The `related_value' field is used to connect related expressions
  317.    (that differ by adding an integer).
  318.    The related expressions are chained in a circular fashion.
  319.    `related_value' is zero for expressions for which this
  320.    chain is not useful.
  321.  
  322.    The `mode' field is usually the same as GET_MODE (`exp'), but
  323.    if `exp' is a CONST_INT and has no machine mode then the `mode'
  324.    field is the mode it was being used as.  Each constant is
  325.    recorded separately for each mode it is used with.  */
  326.  
  327.  
  328. struct table_elt
  329. {
  330.   rtx exp;
  331.   struct table_elt *next_same_hash;
  332.   struct table_elt *prev_same_hash;
  333.   struct table_elt *next_same_value;
  334.   struct table_elt *prev_same_value;
  335.   struct table_elt *first_same_value;
  336.   struct table_elt *related_value;
  337.   int cost;
  338.   enum machine_mode mode;
  339.   char in_memory;
  340.   char in_struct;
  341.   char equivalence_only;
  342. };
  343.  
  344. #define HASH(x, m) (canon_hash (x, m) % NBUCKETS)
  345. /* We don't want a lot of buckets, because we rarely have very many
  346.    things stored in the hash table, and a lot of buckets slows
  347.    down a lot of loops that happen frequently.  */
  348. #define NBUCKETS 31
  349.  
  350. static struct table_elt *table[NBUCKETS];
  351.  
  352. /* Chain of `struct table_elt's made so far for this function
  353.    but currently removed from the table.  */
  354.  
  355. static struct table_elt *free_element_chain;
  356.  
  357. /* Number of `struct table_elt' structures made so far for this function.  */
  358.  
  359. static int n_elements_made;
  360.  
  361. /* Maximum value `n_elements_made' has had so far in this compilation
  362.    for functions previously processed.  */
  363.  
  364. static int max_elements_made;
  365.  
  366. /* Bits describing what kind of values in memory must be invalidated
  367.    for a particular instruction.  If all three bits are zero,
  368.    no memory refs need to be invalidated.  Each bit is more powerful
  369.    than the preceding ones, and if a bit is set then the preceding
  370.    bits are also set.
  371.  
  372.    Here is how the bits are set.
  373.    Writing at a fixed address invalidates only variable addresses,
  374.    writing in a structure element at variable address
  375.      invalidates all but scalar variables,
  376.    and writing in anything else at variable address invalidates everything.  */
  377.  
  378. struct write_data
  379. {
  380.   int var : 1;            /* Invalidate variable addresses.  */
  381.   int nonscalar : 1;        /* Invalidate all but scalar variables.  */
  382.   int all : 1;            /* Invalidate all memory refs.  */
  383. };
  384.  
  385. /* Nonzero if X has the form (PLUS frame-pointer integer).  */
  386.  
  387. #define FIXED_BASE_PLUS_P(X)                    \
  388.   (GET_CODE (X) == PLUS && GET_CODE (XEXP (X, 1)) == CONST_INT    \
  389.    && (XEXP (X, 0) == frame_pointer_rtx || XEXP (X, 0) == arg_pointer_rtx))
  390.  
  391. static struct table_elt *lookup ();
  392. static void free_element ();
  393.  
  394. static void remove_invalid_refs ();
  395. static int exp_equiv_p ();
  396. int refers_to_p ();
  397. int refers_to_mem_p ();
  398. static void invalidate_from_clobbers ();
  399. static int safe_hash ();
  400. static int canon_hash ();
  401. static int get_integer_term ();
  402. static rtx get_related_value ();
  403. static void note_mem_written ();
  404. static int cse_rtx_addr_varies_p ();
  405.  
  406. /* Return an estimate of the cost of computing rtx X.
  407.    The only use of this is to compare the costs of two expressions
  408.    to decide whether to replace one with the other.  */
  409.  
  410. static int
  411. rtx_cost (x)
  412.      rtx x;
  413. {
  414.   register int i, j;
  415.   register RTX_CODE code = GET_CODE (x);
  416.   register char *fmt;
  417.   register int total;
  418.  
  419.   switch (code)
  420.     {
  421.     case REG:
  422.       return 1;
  423.     case SUBREG:
  424.       return 2;
  425.     CONST_COSTS (x, code);
  426.     }
  427.  
  428.   total = 2;
  429.  
  430.   /* Sum the costs of the sub-rtx's, plus 2 just put in.  */
  431.  
  432.   fmt = GET_RTX_FORMAT (code);
  433.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  434.     if (fmt[i] == 'e')
  435.       total += rtx_cost (XEXP (x, i));
  436.     else if (fmt[i] == 'E')
  437.       for (j = 0; j < XVECLEN (x, i); j++)
  438.     total += rtx_cost (XVECEXP (x, i, j));
  439.  
  440.   return total;
  441. }
  442.  
  443. /* Clear the hash table and initialize each register with its own quantity,
  444.    for a new basic block.  */
  445.  
  446. static void
  447. new_basic_block ()
  448. {
  449.   register int i;
  450.   register int vecsize = max_reg * sizeof (rtx);
  451.   next_qty = max_reg;
  452.  
  453.   bzero (reg_rtx, vecsize);
  454.   bzero (reg_tick, vecsize);
  455.  
  456.   bcopy (all_minus_one, reg_in_table, vecsize);
  457.   bcopy (all_minus_one, reg_next_eqv, vecsize);
  458.   bcopy (all_minus_one, reg_prev_eqv, vecsize);
  459.   bcopy (consec_ints, reg_qty, vecsize);
  460.  
  461.   for (i = 0; i < max_qty; i++)
  462.     {
  463.       qty_first_reg[i] = i;
  464.       qty_last_reg[i] = i;
  465.       qty_const[i] = 0;
  466.       qty_const_insn[i] = 0;
  467.     }
  468.  
  469.   for (i = 0; i < NBUCKETS; i++)
  470.     {
  471.       register struct table_elt *this, *next;
  472.       for (this = table[i]; this; this = next)
  473.     {
  474.       next = this->next_same_hash;
  475.       free_element (this);
  476.     }
  477.     }
  478.  
  479.   bzero (table, sizeof table);
  480.  
  481.   prev_insn_cc0 = 0;
  482.   prev_insn = 0;
  483. }
  484.  
  485. /* Say that register REG contains a quantity not in any register before.  */
  486.  
  487. static void
  488. make_new_qty (reg)
  489.      register int reg;
  490. {
  491.   register int q;
  492.  
  493.   q = reg_qty[reg] = next_qty++;
  494.   qty_first_reg[q] = reg;
  495.   qty_last_reg[q] = reg;
  496. }
  497.  
  498. /* Make reg NEW equivalent to reg OLD.
  499.    OLD is not changing; NEW is.  */
  500.  
  501. static void
  502. make_regs_eqv (new, old)
  503.      register int new, old;
  504. {
  505.   register int lastr, firstr;
  506.   register int q = reg_qty[old];
  507.  
  508.   /* Nothing should become eqv until it has a "non-invalid" qty number.  */
  509.   if (q == old)
  510.     abort ();
  511.  
  512.   reg_qty[new] = q;
  513.   firstr = qty_first_reg[q];
  514.   lastr = qty_last_reg[q];
  515.  
  516.   /* Prefer pseudo regs to hard regs with the same value.
  517.      Among pseudos, if NEW will live longer than any other reg of the same qty,
  518.      and that is beyond the current basic block,
  519.      make it the new canonical replacement for this qty.  */
  520.   if (new >= FIRST_PSEUDO_REGISTER
  521.       && (firstr < FIRST_PSEUDO_REGISTER
  522.       || ((regno_last_uid[new] > cse_basic_block_end
  523.            || regno_first_uid[new] < cse_basic_block_start)
  524.           && regno_last_uid[new] > regno_last_uid[firstr])))
  525.     {
  526.       reg_prev_eqv[firstr] = new;
  527.       reg_next_eqv[new] = firstr;
  528.       reg_prev_eqv[new] = -1;
  529.       qty_first_reg[q] = new;
  530.     }
  531.   else
  532.     {
  533.       /* If NEW is a hard reg, insert at end.
  534.      Otherwise, insert before any hard regs that are at the end.  */
  535.       while (lastr < FIRST_PSEUDO_REGISTER && new >= FIRST_PSEUDO_REGISTER)
  536.     lastr = reg_prev_eqv[lastr];
  537.       reg_next_eqv[new] = reg_next_eqv[lastr];
  538.       if (reg_next_eqv[lastr] >= 0)
  539.     reg_prev_eqv[reg_next_eqv[lastr]] = new;
  540.       else
  541.     qty_last_reg[q] = new;
  542.       reg_next_eqv[lastr] = new;
  543.       reg_prev_eqv[new] = lastr;
  544.     }
  545. }
  546.  
  547. /* Discard the records of what is in register REG.  */
  548.  
  549. static void
  550. reg_invalidate (reg)
  551.      register int reg;
  552. {
  553.   register int n = reg_next_eqv[reg];
  554.   register int p = reg_prev_eqv[reg];
  555.   register int q = reg_qty[reg];
  556.  
  557.   reg_tick[reg]++;
  558.  
  559.   if (q == reg)
  560.     {
  561.       /* Save time if already invalid */
  562.       /* It shouldn't be linked to anything if it's invalid.  */
  563.       if (reg_prev_eqv[q] != -1)
  564.     abort ();
  565.       if (reg_next_eqv[q] != -1)
  566.     abort ();
  567.       return;
  568.     }
  569.  
  570.   if (n != -1)
  571.     reg_prev_eqv[n] = p;
  572.   else
  573.     qty_last_reg[q] = p;
  574.   if (p != -1)
  575.     reg_next_eqv[p] = n;
  576.   else
  577.     qty_first_reg[q] = n;
  578.  
  579.   reg_qty[reg] = reg;
  580.   qty_first_reg[reg] = reg;
  581.   qty_last_reg[reg] = reg;
  582.   reg_next_eqv[reg] = -1;
  583.   reg_prev_eqv[reg] = -1;
  584. }
  585.  
  586. /* Remove any invalid expressions from the hash table
  587.    that refer to any of the registers contained in expression X.
  588.  
  589.    Make sure that newly inserted references to those registers
  590.    as subexpressions will be considered valid.
  591.  
  592.    mention_regs is not called when a register itself
  593.    is being stored in the table.  */
  594.  
  595. static void
  596. mention_regs (x)
  597.      rtx x;
  598. {
  599.   register RTX_CODE code = GET_CODE (x);
  600.   register int i, j;
  601.   register char *fmt;
  602.  
  603.   if (code == REG)
  604.     {
  605.       register int regno = REGNO (x);
  606.       reg_rtx[regno] = x;
  607.  
  608.       if (reg_in_table[regno] >= 0 && reg_in_table[regno] != reg_tick[regno])
  609.     remove_invalid_refs (regno);
  610.  
  611.       reg_in_table[regno] = reg_tick[regno];
  612.  
  613.       return;
  614.     }
  615.  
  616.   fmt = GET_RTX_FORMAT (code);
  617.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  618.     if (fmt[i] == 'e')
  619.       mention_regs (XEXP (x, i));
  620.     else if (fmt[i] == 'E')
  621.       for (j = 0; j < XVECLEN (x, i); j++)
  622.     mention_regs (XVECEXP (x, i, j));
  623. }
  624.  
  625. /* Update the register quantities for inserting X into the hash table
  626.    with a value equivalent to CLASSP.
  627.    (If CLASSP is not a REG or a SUBREG, it is irrelevant.)
  628.    If MODIFIED is nonzero, X is a destination; it is being modified.
  629.    Note that reg_invalidate should be called on a register
  630.    before insert_regs is done on that register with MODIFIED != 0.
  631.  
  632.    Nonzero value means that elements of reg_qty have changed
  633.    so X's hash code may be different.  */
  634.  
  635. static int
  636. insert_regs (x, classp, modified)
  637.      rtx x;
  638.      struct table_elt *classp;
  639.      int modified;
  640. {
  641.   if (GET_CODE (x) == REG)
  642.     {
  643.       register int regno = REGNO (x);
  644.       reg_rtx[regno] = x;
  645.       if (modified || reg_qty[regno] == regno)
  646.     {
  647.       if (classp && GET_CODE (classp->exp) == REG)
  648.         {
  649.           make_regs_eqv (regno, REGNO (classp->exp));
  650.           /* Make sure reg_rtx is set up even for regs
  651.          not explicitly set (such as function value).  */
  652.           reg_rtx[REGNO (classp->exp)] = classp->exp;
  653.         }
  654.       else
  655.         make_new_qty (regno);
  656.       return 1;
  657.     }
  658.     }
  659.   /* Copying a subreg into a subreg makes the regs equivalent,
  660.      but only if the entire regs' mode is within one word.
  661.      Copying one reg of a DImode into one reg of another DImode
  662.      does not make them equivalent.  */
  663.   else if (GET_CODE (x) == SUBREG
  664.        && GET_CODE (SUBREG_REG (x)) == REG
  665.        && GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))) <= UNITS_PER_WORD
  666.        && (modified
  667.            || reg_qty[REGNO (SUBREG_REG (x))] == REGNO (SUBREG_REG (x))))
  668.     {
  669.       if (classp && GET_CODE (classp->exp) == SUBREG
  670.       && GET_CODE (SUBREG_REG (classp->exp)) == REG
  671.       && GET_MODE (SUBREG_REG (classp->exp)) == GET_MODE (SUBREG_REG (x)))
  672.     {
  673.       int oregno = REGNO (SUBREG_REG (classp->exp));
  674.       make_regs_eqv (REGNO (SUBREG_REG (x)), oregno);
  675.       /* Make sure reg_rtx is set up even for regs
  676.          not explicitly set (such as function value).  */
  677.       reg_rtx[oregno] = SUBREG_REG (classp->exp);
  678.     }
  679.       else
  680.     make_new_qty (REGNO (SUBREG_REG (x)));
  681.       return 1;
  682.     }
  683.   else
  684.     mention_regs (x);
  685.   return 0;
  686. }
  687.  
  688. /* Look in or update the hash table.  */
  689.  
  690. /* Put the element ELT on the list of free elements.  */
  691.  
  692. static void
  693. free_element (elt)
  694.      struct table_elt *elt;
  695. {
  696.   elt->next_same_hash = free_element_chain;
  697.   free_element_chain = elt;
  698. }
  699.  
  700. /* Return an element that is free for use.  */
  701.  
  702. static struct table_elt *
  703. get_element ()
  704. {
  705.   struct table_elt *elt = free_element_chain;
  706.   if (elt)
  707.     {
  708.       free_element_chain = elt->next_same_hash;
  709.       return elt;
  710.     }
  711.   n_elements_made++;
  712.   return (struct table_elt *) oballoc (sizeof (struct table_elt));
  713. }
  714.  
  715. /* Remove table element ELT from use in the table.
  716.    HASH is its hash code, made using the HASH macro.
  717.    It's an argument because often that is known in advance
  718.    and we save much time not recomputing it.  */
  719.  
  720. static void
  721. remove (elt, hash)
  722.      register struct table_elt *elt;
  723.      int hash;
  724. {
  725.   if (elt == 0)
  726.     return;
  727.  
  728.   /* Mark this element as removed.  See cse_insn.  */
  729.   elt->first_same_value = 0;
  730.  
  731.   /* Remove the table element from its equivalence class.  */
  732.      
  733.   {
  734.     register struct table_elt *prev = elt->prev_same_value;
  735.     register struct table_elt *next = elt->next_same_value;
  736.  
  737.     if (next) next->prev_same_value = prev;
  738.  
  739.     if (prev)
  740.       prev->next_same_value = next;
  741.     else
  742.       {
  743.     register struct table_elt *newfirst = next;
  744.     while (next)
  745.       {
  746.         next->first_same_value = newfirst;
  747.         next = next->next_same_value;
  748.       }
  749.       }
  750.   }
  751.  
  752.   /* Remove the table element from its hash bucket.  */
  753.  
  754.   {
  755.     register struct table_elt *prev = elt->prev_same_hash;
  756.     register struct table_elt *next = elt->next_same_hash;
  757.  
  758.     if (next) next->prev_same_hash = prev;
  759.  
  760.     if (prev)
  761.       prev->next_same_hash = next;
  762.     else
  763.       table[hash] = next; 
  764.   }
  765.  
  766.   /* Remove the table element from its related-value circular chain.  */
  767.  
  768.   if (elt->related_value != 0 && elt->related_value != elt)
  769.     {
  770.       register struct table_elt *p = elt->related_value;
  771.       while (p->related_value != elt)
  772.     p = p->related_value;
  773.       p->related_value = elt->related_value;
  774.       if (p->related_value == p)
  775.     p->related_value = 0;
  776.     }
  777.  
  778.   free_element (elt);
  779. }
  780.  
  781. /* Look up X in the hash table and return its table element,
  782.    or 0 if X is not in the table.
  783.  
  784.    MODE is the machine-mode of X, or if X is an integer constant
  785.    with VOIDmode then MODE is the mode with which X will be used.
  786.  
  787.    Here we are satisfied to find an expression whose tree structure
  788.    looks like X.  */
  789.  
  790. static struct table_elt *
  791. lookup (x, hash, mode)
  792.      rtx x;
  793.      int hash;
  794.      enum machine_mode mode;
  795. {
  796.   register struct table_elt *p;
  797.  
  798.   for (p = table[hash]; p; p = p->next_same_hash)
  799.     if (mode == p->mode && (x == p->exp || exp_equiv_p (x, p->exp, 1)))
  800.       return p;
  801.  
  802.   return 0;
  803. }
  804.  
  805. /* Like `lookup' but don't care whether the table element uses invalid regs.
  806.    Also ignore discrepancies in the machine mode of a register.  */
  807.  
  808. static struct table_elt *
  809. lookup_for_remove (x, hash, mode)
  810.      rtx x;
  811.      int hash;
  812.      enum machine_mode mode;
  813. {
  814.   register struct table_elt *p;
  815.  
  816.   if (GET_CODE (x) == REG)
  817.     {
  818.       int regno = REGNO (x);
  819.       /* Don't check the machine mode when comparing registers;
  820.      invalidating (REG:SI 0) also invalidates (REG:DF 0).  */
  821.       for (p = table[hash]; p; p = p->next_same_hash)
  822.     if (GET_CODE (p->exp) == REG
  823.         && REGNO (p->exp) == regno)
  824.       return p;
  825.     }
  826.   else
  827.     {
  828.       for (p = table[hash]; p; p = p->next_same_hash)
  829.     if (mode == p->mode && (x == p->exp || exp_equiv_p (x, p->exp, 0)))
  830.       return p;
  831.     }
  832.  
  833.   return 0;
  834. }
  835.  
  836. /* Look for an expression equivalent to X and of the form (CODE Y).
  837.    If one is found, return Y.  */
  838.  
  839. static rtx
  840. lookup_as_function (x, code)
  841.      rtx x;
  842.      enum rtx_code code;
  843. {
  844.   register struct table_elt *p = lookup (x, safe_hash (x, 0) % NBUCKETS,
  845.                      GET_MODE (x));
  846.   if (p == 0)
  847.     return 0;
  848.  
  849.   for (p = p->first_same_value; p; p = p->next_same_value)
  850.     {
  851.       if (GET_CODE (p->exp) == code
  852.       /* Make sure this is a valid entry in the table.  */
  853.       && (exp_equiv_p (XEXP (p->exp, 0), XEXP (p->exp, 0), 1)))
  854.     return XEXP (p->exp, 0);
  855.     }
  856.   
  857.   return 0;
  858. }
  859.  
  860. /* Insert X in the hash table, assuming HASH is its hash code
  861.    and CLASSP is the current first element of the class it should go in
  862.    (or 0 if a new class should be made).
  863.    It is inserted at the proper position to keep the class in
  864.    the order cheapest first.
  865.  
  866.    MODE is the machine-mode of X, or if X is an integer constant
  867.    with VOIDmode then MODE is the mode with which X will be used.
  868.  
  869.    For elements of equal cheapness, the most recent one
  870.    goes in front, except that the first element in the list
  871.    remains first unless a cheaper element is added.
  872.  
  873.    The in_memory field in the hash table element is set to 0.
  874.    The caller must set it nonzero if appropriate.
  875.  
  876.    You should call insert_regs (X, CLASSP, MODIFY) before calling here,
  877.    and if insert_regs returns a nonzero value
  878.    you must then recompute its hash code before calling here.
  879.  
  880.    If necessary, update table showing constant values of quantities.  */
  881.  
  882. #define CHEAPER(X,Y)    \
  883.    (((X)->cost < (Y)->cost) ||                        \
  884.     (GET_CODE ((X)->exp) == REG && GET_CODE ((Y)->exp) == REG        \
  885.      && (regno_last_uid[REGNO ((X)->exp)] > cse_basic_block_end        \
  886.      || regno_first_uid[REGNO ((X)->exp)] < cse_basic_block_start)    \
  887.      && (regno_last_uid[REGNO ((X)->exp)]                \
  888.      > regno_last_uid[REGNO ((Y)->exp)])))
  889.  
  890. static struct table_elt *
  891. insert (x, classp, hash, mode)
  892.      register rtx x;
  893.      register struct table_elt *classp;
  894.      int hash;
  895.      enum machine_mode mode;
  896. {
  897.   register struct table_elt *elt;
  898.  
  899.   /* Put an element for X into the right hash bucket.  */
  900.  
  901.   elt = get_element ();
  902.   elt->exp = x;
  903.   elt->cost = rtx_cost (x) * 2;
  904.   /* Make pseudo regs a little cheaper than hard regs.  */
  905.   if (GET_CODE (x) == REG && REGNO (x) >= FIRST_PSEUDO_REGISTER)
  906.     elt->cost -= 1;
  907.   elt->next_same_value = 0;
  908.   elt->prev_same_value = 0;
  909.   elt->next_same_hash = table[hash];
  910.   elt->prev_same_hash = 0;
  911.   elt->related_value = 0;
  912.   elt->in_memory = 0;
  913.   elt->equivalence_only = 0;
  914.   elt->mode = mode;
  915.   if (table[hash])
  916.     table[hash]->prev_same_hash = elt;
  917.   table[hash] = elt;
  918.  
  919.   /* Put it into the proper value-class.  */
  920.   if (classp)
  921.     {
  922.       if (CHEAPER (elt, classp))
  923.     /** Insert at the head of the class */
  924.     {
  925.       register struct table_elt *p;
  926.       elt->next_same_value = classp;
  927.       classp->prev_same_value = elt;
  928.       elt->first_same_value = elt;
  929.  
  930.       for (p = classp; p; p = p->next_same_value)
  931.         p->first_same_value = elt;
  932.     }
  933.       else
  934.     {
  935.       /* Insert not at head of the class.  */
  936.       /* Put it after the last element cheaper than X.  */
  937.       register struct table_elt *p, *next;
  938.       for (p = classp; (next = p->next_same_value) && CHEAPER (next, elt);
  939.            p = next);
  940.       /* Put it after P and before NEXT.  */
  941.       elt->next_same_value = next;
  942.       if (next)
  943.         next->prev_same_value = elt;
  944.       elt->prev_same_value = p;
  945.       p->next_same_value = elt;
  946.       elt->first_same_value = classp;
  947.     }
  948.     }
  949.   else
  950.     elt->first_same_value = elt;
  951.  
  952.   if ((CONSTANT_P (x) || FIXED_BASE_PLUS_P (x))
  953.       && GET_CODE (elt->first_same_value->exp) == REG)
  954.     {
  955.       qty_const[reg_qty[REGNO (elt->first_same_value->exp)]] = x;
  956.       qty_const_insn[reg_qty[REGNO (elt->first_same_value->exp)]] = this_insn;
  957.     }
  958.  
  959.   if (GET_CODE (x) == REG)
  960.     {
  961.       if (elt->next_same_value != 0
  962.       && (CONSTANT_P (elt->next_same_value->exp)
  963.           || FIXED_BASE_PLUS_P (elt->next_same_value->exp)))
  964.     {
  965.       qty_const[reg_qty[REGNO (x)]] = elt->next_same_value->exp;
  966.       qty_const_insn[reg_qty[REGNO (x)]] = this_insn;
  967.     }
  968.       if (CONSTANT_P (elt->first_same_value->exp)
  969.       || FIXED_BASE_PLUS_P (elt->first_same_value->exp))
  970.     {
  971.       qty_const[reg_qty[REGNO (x)]] = elt->first_same_value->exp;
  972.       qty_const_insn[reg_qty[REGNO (x)]] = this_insn;
  973.     }
  974.     }
  975.  
  976.   /* If this is a constant with symbolic value,
  977.      and it has a term with an explicit integer value,
  978.      link it up with related expressions.  */
  979.   if (GET_CODE (x) == CONST)
  980.     {
  981.       rtx subexp = get_related_value (x);
  982.       int subhash;
  983.       struct table_elt *subelt, *subelt_prev;
  984.  
  985.       if (subexp != 0)
  986.     {
  987.       /* Get the integer-free subexpression in the hash table.  */
  988.       subhash = safe_hash (subexp, mode) % NBUCKETS;
  989.       subelt = lookup (subexp, subhash, mode);
  990.       if (subelt == 0)
  991.         subelt = insert (subexp, 0, subhash, mode);
  992.       /* Initialize SUBELT's circular chain if it has none.  */
  993.       if (subelt->related_value == 0)
  994.         subelt->related_value = subelt;
  995.       /* Find the element in the circular chain that precedes SUBELT.  */
  996.       subelt_prev = subelt;
  997.       while (subelt_prev->related_value != subelt)
  998.         subelt_prev = subelt_prev->related_value;
  999.       /* Put new ELT into SUBELT's circular chain just before SUBELT.
  1000.          This way the element that follows SUBELT is the oldest one.  */
  1001.       elt->related_value = subelt_prev->related_value;
  1002.       subelt_prev->related_value = elt;
  1003.     }
  1004.     }
  1005.  
  1006.   return elt;
  1007. }
  1008.  
  1009. /* Remove from the hash table, or mark as invalid,
  1010.    all expressions whose values could be altered by storing in X.
  1011.    X is a register, a subreg, or a memory reference with nonvarying address
  1012.    (because, when a memory reference with a varying address is stored in,
  1013.    all memory references are removed by invalidate_memory
  1014.    so specific invalidation is superfluous).
  1015.  
  1016.    A nonvarying address may be just a register or just
  1017.    a symbol reference, or it may be either of those plus
  1018.    a numeric offset.  */
  1019.  
  1020. static void
  1021. invalidate (x)
  1022.      rtx x;
  1023. {
  1024.   register int i;
  1025.   register struct table_elt *p;
  1026.   register rtx base;
  1027.   register int start, end;
  1028.  
  1029.   /* If X is a register, dependencies on its contents
  1030.      are recorded through the qty number mechanism.
  1031.      Just change the qty number of the register,
  1032.      mark it as invalid for expressions that refer to it,
  1033.      and remove it itself.  */
  1034.  
  1035.   if (GET_CODE (x) == REG)
  1036.     {
  1037.       register int hash = HASH (x, 0);
  1038.       reg_invalidate (REGNO (x));
  1039.       remove (lookup_for_remove (x, hash, GET_MODE (x)), hash);
  1040.       return;
  1041.     }
  1042.  
  1043.   if (GET_CODE (x) == SUBREG)
  1044.     {
  1045.       if (GET_CODE (SUBREG_REG (x)) != REG)
  1046.     abort ();
  1047.       invalidate (SUBREG_REG (x));
  1048.       return;
  1049.     }
  1050.  
  1051.   /* X is not a register; it must be a memory reference with
  1052.      a nonvarying address.  Remove all hash table elements
  1053.      that refer to overlapping pieces of memory.  */
  1054.  
  1055.   if (GET_CODE (x) != MEM)
  1056.     abort ();
  1057.   base = XEXP (x, 0);
  1058.   start = 0;
  1059.  
  1060.   /* Registers with nonvarying addresses usually have constant equivalents;
  1061.      but the frame pointer register is also possible.  */
  1062.   if (GET_CODE (base) == REG
  1063.       && qty_const[reg_qty[REGNO (base)]] != 0)
  1064.     base = qty_const[reg_qty[REGNO (base)]];
  1065.  
  1066.   if (GET_CODE (base) == CONST)
  1067.     base = XEXP (base, 0);
  1068.   if (GET_CODE (base) == PLUS
  1069.       && GET_CODE (XEXP (base, 1)) == CONST_INT)
  1070.     {
  1071.       start = INTVAL (XEXP (base, 1));
  1072.       base = XEXP (base, 0);
  1073.     }
  1074.  
  1075.   end = start + GET_MODE_SIZE (GET_MODE (x));
  1076.   for (i = 0; i < NBUCKETS; i++)
  1077.     {
  1078.       register struct table_elt *next;
  1079.       for (p = table[i]; p; p = next)
  1080.     {
  1081.       next = p->next_same_hash;
  1082.       if (refers_to_mem_p (p->exp, base, start, end))
  1083.         remove (p, i);
  1084.     }
  1085.     }
  1086. }
  1087.  
  1088. /* Remove all expressions that refer to register REGNO,
  1089.    since they are already invalid, and we are about to
  1090.    mark that register valid again and don't want the old
  1091.    expressions to reappear as valid.  */
  1092.  
  1093. static void
  1094. remove_invalid_refs (regno)
  1095.      int regno;
  1096. {
  1097.   register int i;
  1098.   register struct table_elt *p, *next;
  1099.   register rtx x = reg_rtx[regno];
  1100.  
  1101.   for (i = 0; i < NBUCKETS; i++)
  1102.     for (p = table[i]; p; p = next)
  1103.       {
  1104.     next = p->next_same_hash;
  1105.     if (GET_CODE (p->exp) != REG && refers_to_p (p->exp, x))
  1106.       remove (p, i);
  1107.       }
  1108. }
  1109.  
  1110. /* Remove from the hash table all expressions that reference memory,
  1111.    or some of them as specified by *WRITES.  */
  1112.  
  1113. static void
  1114. invalidate_memory (writes)
  1115.      struct write_data *writes;
  1116. {
  1117.   register int i;
  1118.   register struct table_elt *p, *next;
  1119.   int all = writes->all;
  1120.   int nonscalar = writes->nonscalar;
  1121.  
  1122.   for (i = 0; i < NBUCKETS; i++)
  1123.     for (p = table[i]; p; p = next)
  1124.       {
  1125.     next = p->next_same_hash;
  1126.     if (p->in_memory
  1127.         && (all
  1128.         || (nonscalar && p->in_struct)
  1129.         || cse_rtx_addr_varies_p (p->exp)))
  1130.       remove (p, i);
  1131.       }
  1132. }
  1133.  
  1134. /* Return the value of the integer term in X, if one is apparent;
  1135.    otherwise return 0.
  1136.    We do not check extremely carefully for the presence of integer terms
  1137.    but rather consider only the cases that `insert' notices
  1138.    for the `related_value' field.  */
  1139.  
  1140. static int
  1141. get_integer_term (x)
  1142.      rtx x;
  1143. {
  1144.   if (GET_CODE (x) == CONST)
  1145.     x = XEXP (x, 0);
  1146.  
  1147.   if (GET_CODE (x) == MINUS
  1148.       && GET_CODE (XEXP (x, 1)) == CONST_INT)
  1149.     return - INTVAL (XEXP (x, 1));
  1150.   if (GET_CODE (x) != PLUS)
  1151.     return 0;
  1152.   if (GET_CODE (XEXP (x, 0)) == CONST_INT)
  1153.     return INTVAL (XEXP (x, 0));
  1154.   if (GET_CODE (XEXP (x, 1)) == CONST_INT)
  1155.     return INTVAL (XEXP (x, 1));
  1156.   return 0;
  1157. }
  1158.  
  1159. static rtx
  1160. get_related_value (x)
  1161.      rtx x;
  1162. {
  1163.   if (GET_CODE (x) != CONST)
  1164.     return 0;
  1165.   x = XEXP (x, 0);
  1166.   if (GET_CODE (x) == PLUS)
  1167.     {
  1168.       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
  1169.     return XEXP (x, 1);
  1170.       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
  1171.     return XEXP (x, 0);
  1172.     }
  1173.   else if (GET_CODE (x) == MINUS
  1174.        && GET_CODE (XEXP (x, 1)) == CONST_INT)
  1175.     return XEXP (x, 0);
  1176.   return 0;
  1177. }
  1178.  
  1179. /* Given an expression X of type CONST,
  1180.    and ELT which is its table entry (or 0 if it
  1181.    is not in the hash table),
  1182.    return an alternate expression for X as a register plus integer.
  1183.    If none can be found or it would not be a valid address, return 0.  */
  1184.  
  1185. static rtx
  1186. use_related_value (x, elt)
  1187.      rtx x;
  1188.      struct table_elt *elt;
  1189. {
  1190.   register struct table_elt *relt = 0;
  1191.   register struct table_elt *p;
  1192.   int offset;
  1193.   rtx addr;
  1194.  
  1195.   /* First, is there anything related known?
  1196.      If we have a table element, we can tell from that.
  1197.      Otherwise, must look it up.  */
  1198.  
  1199.   if (elt != 0 && elt->related_value != 0)
  1200.     relt = elt;
  1201.   else if (elt == 0 && GET_CODE (x) == CONST)
  1202.     {
  1203.       rtx subexp = get_related_value (x);
  1204.       if (subexp != 0)
  1205.     relt = lookup (subexp,
  1206.                safe_hash (subexp, GET_MODE (subexp)) % NBUCKETS,
  1207.                GET_MODE (subexp));
  1208.     }
  1209.  
  1210.   if (relt == 0)
  1211.     return 0;
  1212.  
  1213.   /* Search all related table entries for one that has an
  1214.      equivalent register.  */
  1215.  
  1216.   p = relt;
  1217.   while (1)
  1218.     {
  1219.       if (p->first_same_value != 0
  1220.       && GET_CODE (p->first_same_value->exp) == REG)
  1221.     break;
  1222.       p = p->related_value;
  1223.  
  1224.       /* We went all the way around, so there is nothing to be found.
  1225.      Return failure.  */
  1226.       if (p == relt)
  1227.     return 0;
  1228.       /* Perhaps RELT was in the table for some other reason and
  1229.      it has no related values recorded.  */
  1230.       if (p == 0)
  1231.     return 0;
  1232.     }
  1233.  
  1234.   offset = (get_integer_term (x) - get_integer_term (p->exp));
  1235.   if (offset == 0)
  1236.     abort ();
  1237.  
  1238.   addr = plus_constant (p->first_same_value->exp, offset);
  1239.   if (memory_address_p (QImode, addr))
  1240.     return addr;
  1241.   return 0;
  1242. }
  1243.  
  1244. /* Hash an rtx.  We are careful to make sure the value is never negative.
  1245.    Equivalent registers hash identically.
  1246.  
  1247.    Store 1 in do_not_record if any subexpression is volatile.
  1248.  
  1249.    Store 1 in hash_arg_in_memory if X contains a MEM rtx
  1250.    which does not have the ->unchanging bit set.
  1251.    In this case, also store 1 in hash_arg_in_struct
  1252.    if there is a MEM rtx which has the ->in_struct bit set.
  1253.  
  1254.    Note that cse_insn knows that the hash code of a MEM expression
  1255.    is just (int) MEM plus the hash code of the address.
  1256.    It also knows it can use HASHREG to get the hash code of (REG n).  */
  1257.  
  1258. #define HASHBITS 16
  1259.  
  1260. #define HASHREG(RTX) \
  1261.  ((((int) REG << 7) + reg_qty[REGNO (RTX)]) % NBUCKETS)
  1262.  
  1263. static int
  1264. canon_hash (x, mode)
  1265.      rtx x;
  1266.      enum machine_mode mode;
  1267. {
  1268.   register int i, j;
  1269.   register int hash = 0;
  1270.   register RTX_CODE code;
  1271.   register char *fmt;
  1272.  
  1273.   /* repeat is used to turn tail-recursion into iteration.  */
  1274.  repeat:
  1275.   code = GET_CODE (x);
  1276.   switch (code)
  1277.     {
  1278.     case REG:
  1279.       {
  1280.     /* We do not invalidate anything on pushing or popping
  1281.        because they cannot change anything but the stack pointer;
  1282.        but that means we must consider the stack pointer volatile
  1283.        since it can be changed "mysteriously".  */
  1284.  
  1285.     register int regno = REGNO (x);
  1286.     if (regno == STACK_POINTER_REGNUM)
  1287.       {
  1288.         do_not_record = 1;
  1289.         return 0;
  1290.       }
  1291.     return hash + ((int) REG << 7) + reg_qty[regno];
  1292.       }
  1293.  
  1294.     case CONST_INT:
  1295.       hash += ((int) mode + ((int) CONST_INT << 7)
  1296.            + INTVAL (x) + INTVAL (x) >> HASHBITS);
  1297.       return ((1 << HASHBITS) - 1) & hash;
  1298.  
  1299.       /* Assume there is only one rtx object for any given label.  */
  1300.     case LABEL_REF:
  1301.       return hash + ((int) LABEL_REF << 7) + (int) XEXP (x, 0);
  1302.  
  1303.     case SYMBOL_REF:
  1304.       return hash + ((int) SYMBOL_REF << 7) + (int) XEXP (x, 0);
  1305.  
  1306.     case MEM:
  1307.       if (x->volatil)
  1308.     {
  1309.       do_not_record = 1;
  1310.       return 0;
  1311.     }
  1312.       if (! x->unchanging)
  1313.     {
  1314.       hash_arg_in_memory = 1;
  1315.       if (x->in_struct) hash_arg_in_struct = 1;
  1316.     }
  1317.       /* Now that we have already found this special case,
  1318.      might as well speed it up as much as possible.  */
  1319.       hash += (int) MEM;
  1320.       x = XEXP (x, 0);
  1321.       goto repeat;
  1322.  
  1323.     case PRE_DEC:
  1324.     case PRE_INC:
  1325.     case POST_DEC:
  1326.     case POST_INC:
  1327.     case PC:
  1328.     case CC0:
  1329.     case CALL:
  1330.       do_not_record = 1;
  1331.       return 0;
  1332.  
  1333.     case ASM_OPERANDS:
  1334.       if (x->volatil)
  1335.     {
  1336.       do_not_record = 1;
  1337.       return 0;
  1338.     }
  1339.     }
  1340.  
  1341.   i = GET_RTX_LENGTH (code) - 1;
  1342.   hash += (int) code + (int) GET_MODE (x);
  1343.   fmt = GET_RTX_FORMAT (code);
  1344.   for (; i >= 0; i--)
  1345.     {
  1346.       if (fmt[i] == 'e')
  1347.     {
  1348.       /* If we are about to do the last recursive call
  1349.          needed at this level, change it into iteration.
  1350.          This function  is called enough to be worth it.  */
  1351.       if (i == 0)
  1352.         {
  1353.           x = XEXP (x, 0);
  1354.           goto repeat;
  1355.         }
  1356.       hash += canon_hash (XEXP (x, i), 0);
  1357.     }
  1358.       else if (fmt[i] == 'E')
  1359.     for (j = 0; j < XVECLEN (x, i); j++)
  1360.       hash += canon_hash (XVECEXP (x, i, j), 0);
  1361.       else if (fmt[i] == 's')
  1362.     {
  1363.       register char *p = XSTR (x, i);
  1364.       while (*p)
  1365.         {
  1366.           register int tem = *p++;
  1367.           hash += ((1 << HASHBITS) - 1) & (tem + tem >> HASHBITS);
  1368.         }
  1369.     }
  1370.       else
  1371.     {
  1372.       register int tem = XINT (x, i);
  1373.       hash += ((1 << HASHBITS) - 1) & (tem + tem >> HASHBITS);
  1374.     }
  1375.     }
  1376.   return hash;
  1377. }
  1378.  
  1379. /* Like canon_hash but with no side effects.  */
  1380.  
  1381. static int
  1382. safe_hash (x, mode)
  1383.      rtx x;
  1384.      enum machine_mode mode;
  1385. {
  1386.   int save_do_not_record = do_not_record;
  1387.   int save_hash_arg_in_memory = hash_arg_in_memory;
  1388.   int save_hash_arg_in_struct = hash_arg_in_struct;
  1389.   int hash = canon_hash (x, mode);
  1390.   hash_arg_in_memory = save_hash_arg_in_memory;
  1391.   hash_arg_in_struct = save_hash_arg_in_struct;
  1392.   do_not_record = save_do_not_record;
  1393.   return hash;
  1394. }
  1395.  
  1396. /* Return 1 iff X and Y would canonicalize into the same thing,
  1397.    without actually constructing the canonicalization of either one.
  1398.    If VALIDATE is nonzero,
  1399.    we assume X is an expression being processed from the rtl
  1400.    and Y was found in the hash table.  We check register refs
  1401.    in Y for being marked as valid.  */
  1402.  
  1403. static int
  1404. exp_equiv_p (x, y, validate)
  1405.      rtx x, y;
  1406.      int validate;
  1407. {
  1408.   register int i;
  1409.   register RTX_CODE code = GET_CODE (x);
  1410.   register char *fmt;
  1411.  
  1412.   /* Note: it is incorrect to assume an expression is equivalent to itself
  1413.      if VALIDATE is nonzero.  */
  1414.   if (x == y && !validate)
  1415.     return 1;
  1416.   if (code != GET_CODE (y))
  1417.     return 0;
  1418.  
  1419.   switch (code)
  1420.     {
  1421.     case PC:
  1422.     case CC0:
  1423.       return x == y;
  1424.  
  1425.     case CONST_INT:
  1426.       return XINT (x, 0) == XINT (y, 0);
  1427.  
  1428.     case LABEL_REF:
  1429.     case SYMBOL_REF:
  1430.       return XEXP (x, 0) == XEXP (y, 0);
  1431.  
  1432.     case REG:
  1433.       return (reg_qty[REGNO (x)] == reg_qty[REGNO (y)]
  1434.           && (!validate
  1435.           || reg_in_table[REGNO (y)] == reg_tick[REGNO (y)]));
  1436.     }
  1437.  
  1438.   /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.  */
  1439.  
  1440.   if (GET_MODE (x) != GET_MODE (y))
  1441.     return 0;
  1442.  
  1443.   /* Compare the elements.  If any pair of corresponding elements
  1444.      fail to match, return 0 for the whole things.  */
  1445.  
  1446.   fmt = GET_RTX_FORMAT (code);
  1447.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  1448.     {
  1449.       if (fmt[i] == 'e')
  1450.     {
  1451.       if (! exp_equiv_p (XEXP (x, i), XEXP (y, i), validate))
  1452.         return 0;
  1453.     }
  1454.       else if (fmt[i] == 'E')
  1455.     {
  1456.       int j;
  1457.       for (j = 0; j < XVECLEN (x, i); j++)
  1458.         if (! exp_equiv_p (XVECEXP (x, i, j), XVECEXP (y, i, j), validate))
  1459.           return 0;
  1460.     }
  1461.       else if (fmt[i] == 's')
  1462.     {
  1463.       if (strcmp (XSTR (x, i), XSTR (y, i)))
  1464.         return 0;
  1465.     }
  1466.       else
  1467.     {
  1468.       if (XINT (x, i) != XINT (y, i))
  1469.         return 0;
  1470.     }
  1471.     }
  1472.   return 1;
  1473. }
  1474.  
  1475. /* Return 1 iff any subexpression of X matches Y.
  1476.    Here we do not require that X or Y be valid (for registers referred to)
  1477.    for being in the hash table.  */
  1478.  
  1479. int
  1480. refers_to_p (x, y)
  1481.      rtx x, y;
  1482. {
  1483.   register int i;
  1484.   register RTX_CODE code;
  1485.   register char *fmt;
  1486.  
  1487.  repeat:
  1488.   if (x == y)
  1489.     return 1;
  1490.  
  1491.   code = GET_CODE (x);
  1492.   /* If X as a whole has the same code as Y, they may match.
  1493.      If so, return 1.  */
  1494.   if (code == GET_CODE (y))
  1495.     {
  1496.       if (exp_equiv_p (x, y, 0))
  1497.     return 1;
  1498.     }
  1499.  
  1500.   /* X does not match, so try its subexpressions.  */
  1501.  
  1502.   fmt = GET_RTX_FORMAT (code);
  1503.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  1504.     if (fmt[i] == 'e')
  1505.       {
  1506.     if (i == 0)
  1507.       {
  1508.         x = XEXP (x, 0);
  1509.         goto repeat;
  1510.       }
  1511.     else
  1512.       if (refers_to_p (XEXP (x, i), y))
  1513.         return 1;
  1514.       }
  1515.     else if (fmt[i] == 'E')
  1516.       {
  1517.     int j;
  1518.     for (j = 0; j < XVECLEN (x, i); j++)
  1519.       if (refers_to_p (XVECEXP (x, i, j), y))
  1520.         return 1;
  1521.       }
  1522.  
  1523.   return 0;
  1524. }
  1525.  
  1526. /* Return 1 iff any subexpression of X refers to memory
  1527.    at an address of REG plus some offset
  1528.    such that any of the bytes' offsets fall between START (inclusive)
  1529.    and END (exclusive).
  1530.  
  1531.    The value is undefined if X is a varying address.
  1532.    This function is not used in such cases.
  1533.  
  1534.    When used in the cse pass, `qty_const' is nonzero, and it is used
  1535.    to treat an address that is a register with a known constant value
  1536.    as if it were that constant value.
  1537.    In the loop pass, `qty_const' is zero, so this is not done.  */
  1538.  
  1539. int
  1540. refers_to_mem_p (x, reg, start, end)
  1541.      rtx x, reg;
  1542.      int start, end;
  1543. {
  1544.   register int i;
  1545.   register RTX_CODE code;
  1546.   register char *fmt;
  1547.  
  1548.  repeat:
  1549.   code = GET_CODE (x);
  1550.   if (code == MEM)
  1551.     {
  1552.       register rtx addr = XEXP (x, 0);    /* Get the address.  */
  1553.       int myend;
  1554.       if (GET_CODE (addr) == REG
  1555.       /* qty_const is 0 when outside the cse pass;
  1556.          at such times, this info is not available.  */
  1557.       && qty_const != 0
  1558.       && qty_const[reg_qty[REGNO (addr)]] != 0)
  1559.     addr = qty_const[reg_qty[REGNO (addr)]];
  1560.       if (GET_CODE (addr) == CONST)
  1561.     addr = XEXP (addr, 0);
  1562.  
  1563.       /* If ADDR is BASE, or BASE plus an integer, put
  1564.      the integer in I.  */
  1565.       if (addr == reg)
  1566.     i = 0;
  1567.       else if (GET_CODE (addr) == PLUS
  1568.            && XEXP (addr, 0) == reg
  1569.            && GET_CODE (XEXP (addr, 1)) == CONST_INT)
  1570.     i = INTVAL (XEXP (addr, 1));
  1571.       else
  1572.     return 0;
  1573.  
  1574.       myend = i + GET_MODE_SIZE (GET_MODE (x));
  1575.       return myend > start && i < end;
  1576.     }
  1577.  
  1578.   /* X does not match, so try its subexpressions.  */
  1579.  
  1580.   fmt = GET_RTX_FORMAT (code);
  1581.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  1582.     if (fmt[i] == 'e')
  1583.       {
  1584.     if (i == 0)
  1585.       {
  1586.         x = XEXP (x, 0);
  1587.         goto repeat;
  1588.       }
  1589.     else
  1590.       if (refers_to_mem_p (XEXP (x, i), reg, start, end))
  1591.         return 1;
  1592.       }
  1593.     else if (fmt[i] == 'E')
  1594.       {
  1595.     int j;
  1596.     for (j = 0; j < XVECLEN (x, i); j++)
  1597.       if (refers_to_mem_p (XVECEXP (x, i, j), reg, start, end))
  1598.         return 1;
  1599.       }
  1600.  
  1601.   return 0;
  1602. }
  1603.  
  1604. /* Nonzero if X refers to memory at a varying address;
  1605.    except that a register which has at the moment a known constant value
  1606.    isn't considered variable.  */
  1607.  
  1608. static int
  1609. cse_rtx_addr_varies_p (x)
  1610.      rtx x;
  1611. {
  1612.   if (GET_CODE (x) == MEM
  1613.       && GET_CODE (XEXP (x, 0)) == REG
  1614.       && qty_const[reg_qty[REGNO (XEXP (x, 0))]] != 0)
  1615.     return 0;
  1616.   return rtx_addr_varies_p (x);
  1617. }
  1618.  
  1619. /* Canonicalize an expression:
  1620.    replace each register reference inside it
  1621.    with the "oldest" equivalent register.  */
  1622.  
  1623. static rtx
  1624. canon_reg (x)
  1625.      rtx x;
  1626. {
  1627.   register int i;
  1628.   register RTX_CODE code = GET_CODE (x);
  1629.   register char *fmt;
  1630.  
  1631.   switch (code)
  1632.     {
  1633.     case PC:
  1634.     case CC0:
  1635.     case CONST:
  1636.     case CONST_INT:
  1637.     case CONST_DOUBLE:
  1638.     case SYMBOL_REF:
  1639.     case LABEL_REF:
  1640.     case ADDR_VEC:
  1641.     case ADDR_DIFF_VEC:
  1642.       return x;
  1643.  
  1644.     case REG:
  1645.       {
  1646.     register rtx new;
  1647.     /* Never replace a hard reg, because hard regs can appear
  1648.        in more than one machine mode, and we must preserve the mode
  1649.        of each occurrence.  Also, some hard regs appear in
  1650.        MEMs that are shared and mustn't be altered.  */
  1651.     if (REGNO (x) < FIRST_PSEUDO_REGISTER)
  1652.       return x;
  1653.     new = reg_rtx[qty_first_reg[reg_qty[REGNO (x)]]];
  1654.     return new ? new : x;
  1655.       }
  1656.     }
  1657.  
  1658.   fmt = GET_RTX_FORMAT (code);
  1659.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  1660.     {
  1661.       register int j;
  1662.  
  1663.       if (fmt[i] == 'e')
  1664.     XEXP (x, i) = canon_reg (XEXP (x, i));
  1665.       else if (fmt[i] == 'E')
  1666.     for (j = 0; j < XVECLEN (x, i); j++)
  1667.       XVECEXP (x, i, j) = canon_reg (XVECEXP (x, i, j));
  1668.     }
  1669.  
  1670.   return x;
  1671. }
  1672.  
  1673. /* If X is a nontrivial arithmetic operation on an argument
  1674.    for which a constant value can be determined, return
  1675.    the result of operating on that value, as a constant.
  1676.    Otherwise, return X, possibly with one or more operands
  1677.    modified by recursive calls to this function.
  1678.  
  1679.    If X is a register whose contents are known, we do NOT
  1680.    return those contents.  This is because an instruction that
  1681.    uses a register is usually faster than one that uses a constant.
  1682.  
  1683.    COPYFLAG is nonzero for memory addresses and subexpressions thereof.
  1684.    If COPYFLAG is nonzero, we avoid altering X itself
  1685.    by creating new structure when necessary.  In this case we
  1686.    can risk creating invalid structure because it will be tested.
  1687.    If COPYFLAG is zero, be careful not to substitute constants
  1688.    into expressions that cannot be simplified.  */
  1689.  
  1690. static rtx
  1691. fold_rtx (x, copyflag)
  1692.      rtx x;
  1693.      int copyflag;
  1694. {
  1695.   register RTX_CODE code = GET_CODE (x);
  1696.   register char *fmt;
  1697.   register int i, val;
  1698.   rtx new = 0;
  1699.   int copied = ! copyflag;
  1700.   int width = GET_MODE_BITSIZE (GET_MODE (x));
  1701.  
  1702.   /* Constant equivalents of first three operands of X;
  1703.      0 when no such equivalent is known.  */
  1704.   rtx const_arg0;
  1705.   rtx const_arg1;
  1706.   rtx const_arg2;
  1707.  
  1708.   switch (code)
  1709.     {
  1710.     case CONST:
  1711.     case CONST_INT:
  1712.     case CONST_DOUBLE:
  1713.     case SYMBOL_REF:
  1714.     case LABEL_REF:
  1715.     case PC:
  1716.     case CC0:
  1717.     case REG:
  1718.       return x;
  1719.  
  1720.   /* We must be careful when folding a memory address
  1721.      to avoid making it invalid.  So fold nondestructively
  1722.      and use the result only if it's valid.  */
  1723.     case MEM:
  1724.       {
  1725.     rtx newaddr = fold_rtx (XEXP (x, 0), 1);
  1726.     /* Save time if no change was made.  */
  1727.     if (XEXP (x, 0) == newaddr)
  1728.       return x;
  1729.  
  1730.     if (! memory_address_p (GET_MODE (x), newaddr)
  1731.         && memory_address_p (GET_MODE (x), XEXP (x, 0)))
  1732.       return x;
  1733.  
  1734.     /* Don't replace a value with a more expensive one.  */
  1735.     if (rtx_cost (XEXP (x, 0)) < rtx_cost (newaddr))
  1736.       return x;
  1737.  
  1738.     if (copyflag)
  1739.       return gen_rtx (MEM, GET_MODE (x), newaddr);
  1740.     XEXP (x, 0) = newaddr;
  1741.     return x;
  1742.       }
  1743.     }
  1744.  
  1745.   const_arg0 = 0;
  1746.   const_arg1 = 0;
  1747.   const_arg2 = 0;
  1748.  
  1749.   /* Try folding our operands.
  1750.      Then see which ones have constant values known.  */
  1751.  
  1752.   fmt = GET_RTX_FORMAT (code);
  1753.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  1754.     if (fmt[i] == 'e')
  1755.       {
  1756.     register rtx tem = fold_rtx (XEXP (x, i), copyflag);
  1757.  
  1758.     /* If an operand has changed under folding, and we are not supposed to
  1759.        alter the original structure, copy X if we haven't yet done so.  */
  1760.     if (! copied && tem != XEXP (x, i))
  1761.       {
  1762.         int j;
  1763.         rtx new = rtx_alloc (code);
  1764.         PUT_MODE (new, GET_MODE (x));
  1765.         for (j = 0; j < GET_RTX_LENGTH (code); j++)
  1766.           XINT (new, j) = XINT (x, j);
  1767.         x = new;
  1768.         copied = 1;
  1769.       }
  1770.  
  1771.     /* Install the possibly altered folded operand.  */
  1772.     XEXP (x, i) = tem;
  1773.  
  1774.     /* For the first three operands, see if the operand
  1775.        is constant or equivalent to a constant.  */
  1776.     if (i < 3)
  1777.       {
  1778.         rtx tem1;
  1779.         rtx const_arg = 0;
  1780.  
  1781.         if (CONSTANT_P (tem))
  1782.           const_arg = tem;
  1783.         else if (GET_CODE (tem) == REG
  1784.              && qty_const[reg_qty[REGNO (tem)]] != 0
  1785.              /* Make sure it is really a constant */
  1786.              && ((tem1 = qty_const[reg_qty[REGNO (tem)]]),
  1787.              GET_CODE (tem1) != REG && GET_CODE (tem1) != PLUS))
  1788.           const_arg = qty_const[reg_qty[REGNO (tem)]];
  1789.  
  1790.         switch (i)
  1791.           {
  1792.           case 0:
  1793.         const_arg0 = const_arg;
  1794.         break;
  1795.           case 1:
  1796.         const_arg1 = const_arg;
  1797.         break;
  1798.           case 2:
  1799.         const_arg2 = const_arg;
  1800.         break;
  1801.           }
  1802.       }
  1803.       }
  1804.     else if (fmt[i] == 'E')
  1805.       /* Don't try to fold inside of a vector of expressions.
  1806.      Doing nothing is is harmless.  */
  1807.       ;
  1808.  
  1809.   /* Now decode the kind of rtx X is
  1810.      and either return X (if nothing can be done)
  1811.      or store a value in VAL and drop through
  1812.      (to return a CONST_INT for the integer VAL).  */
  1813.  
  1814.   if (GET_RTX_LENGTH (code) == 1)
  1815.     {
  1816.       register int arg0;
  1817.  
  1818.       if (const_arg0 == 0 || GET_CODE (const_arg0) != CONST_INT)
  1819.     return x;
  1820.  
  1821.       arg0 = INTVAL (const_arg0);
  1822.  
  1823.       switch (GET_CODE (x))
  1824.     {
  1825.     case NOT:
  1826.       val = ~ arg0;
  1827.       break;
  1828.  
  1829.     case NEG:
  1830.       val = - arg0;
  1831.       break;
  1832.  
  1833.     case TRUNCATE:
  1834.       val = arg0;
  1835.       break;
  1836.  
  1837.     case ZERO_EXTEND:
  1838.       {
  1839.         enum machine_mode mode = GET_MODE (XEXP (x, 0));
  1840.         if (mode == VOIDmode)
  1841.           return x;
  1842.         if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_INT)
  1843.           val = arg0 & ~((-1) << GET_MODE_BITSIZE (mode));
  1844.         break;
  1845.       }
  1846.  
  1847.     case SIGN_EXTEND:
  1848.       {
  1849.         enum machine_mode mode = GET_MODE (XEXP (x, 0));
  1850.         if (mode == VOIDmode)
  1851.           return x;
  1852.         if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_INT)
  1853.           {
  1854.         val = arg0 & ~((-1) << GET_MODE_BITSIZE (mode));
  1855.         if (val & (1 << (GET_MODE_BITSIZE (mode) - 1)))
  1856.           val -= 1 << GET_MODE_BITSIZE (mode);
  1857.           }
  1858.         break;
  1859.       }
  1860.  
  1861.     default:
  1862.       return x;
  1863.     }
  1864.     }
  1865.   else if (GET_RTX_LENGTH (code) == 2)
  1866.     {
  1867.       register int arg0, arg1, arg0s, arg1s;
  1868.  
  1869.       /* If 1st arg is the condition codes, 2nd must be zero
  1870.      and this must be a comparison.
  1871.      Decode the info on how the previous insn set the cc0
  1872.      and use that to deduce result of comparison.  */
  1873.       if (XEXP (x, 0) == cc0_rtx)
  1874.     {
  1875.       if (prev_insn_cc0 == 0
  1876.           || const_arg1 != const0_rtx
  1877.           /* 0200 bit in prev_insn_cc0 means only zeroness is known,
  1878.          and sign is not known.  */
  1879.           || ((prev_insn_cc0 & 0200)
  1880.           && code != EQ && code != NE))
  1881.         return x;
  1882.       if (code == LEU || code == LTU || code == GEU || code == GTU)
  1883.         arg0 = prev_insn_cc0 & 7;
  1884.       else
  1885.         arg0 = (prev_insn_cc0 >> 3) & 7;
  1886.       if (arg0 == 7) arg0 = -1;
  1887.  
  1888.       switch (code)
  1889.         {
  1890.         case LE:
  1891.         case LEU:
  1892.           return (arg0 <= 0) ? const1_rtx : const0_rtx;
  1893.         case LT:
  1894.         case LTU:
  1895.           return (arg0 < 0) ? const1_rtx : const0_rtx;
  1896.         case GE:
  1897.         case GEU:
  1898.           return (arg0 >= 0) ? const1_rtx : const0_rtx;
  1899.         case GT:
  1900.         case GTU:
  1901.           return (arg0 > 0) ? const1_rtx : const0_rtx;
  1902.         case NE:
  1903.           return (arg0 != 0) ? const1_rtx : const0_rtx;
  1904.         case EQ:
  1905.           return (arg0 == 0) ? const1_rtx : const0_rtx;
  1906.         default:
  1907.           abort ();
  1908.         }
  1909.     }
  1910.  
  1911.       if (const_arg0 == 0 || const_arg1 == 0
  1912.       || GET_CODE (const_arg0) != CONST_INT
  1913.       || GET_CODE (const_arg1) != CONST_INT)
  1914.     {
  1915.       /* Even if we can't compute a constant result,
  1916.          there are some cases worth simplifying.  */
  1917.       if (code == PLUS)
  1918.         {
  1919.           if (const_arg0 == const0_rtx)
  1920.         return XEXP (x, 1);
  1921.           if (const_arg1 == const0_rtx)
  1922.         return XEXP (x, 0);
  1923.  
  1924.           /* Handle both-operands-constant cases.  */
  1925.           if (const_arg0 != 0 && const_arg1 != 0)
  1926.             {
  1927.           if (GET_CODE (const_arg0) == CONST_INT)
  1928.             new = plus_constant (const_arg1, INTVAL (const_arg0));
  1929.           else if (GET_CODE (const_arg1) == CONST_INT)
  1930.             new = plus_constant (const_arg0, INTVAL (const_arg1));
  1931.           else
  1932.             {
  1933.               new = gen_rtx (PLUS, GET_MODE (x), const0_rtx, const0_rtx);
  1934.               XEXP (new, 0) = const_arg0;
  1935.               if (GET_CODE (const_arg0) == CONST)
  1936.             XEXP (new, 0) = XEXP (const_arg0, 0);
  1937.               XEXP (new, 1) = const_arg1;
  1938.               if (GET_CODE (const_arg1) == CONST)
  1939.             XEXP (new, 1) = XEXP (const_arg1, 0);
  1940.               new = gen_rtx (CONST, GET_MODE (new), new);
  1941.             }
  1942.         }
  1943.  
  1944.           else if (const_arg0 != 0
  1945.                && GET_CODE (const_arg0) == CONST_INT
  1946.                && GET_CODE (XEXP (x, 1)) == PLUS
  1947.                && (CONSTANT_P (XEXP (XEXP (x, 1), 0))
  1948.                || CONSTANT_P (XEXP (XEXP (x, 1), 1))))
  1949.         /* constant + (variable + constant)
  1950.            can result if an index register is made constant.
  1951.            We simplify this by adding the constants.
  1952.            If we did not, it would become an invalid address.  */
  1953.         new = plus_constant (XEXP (x, 1),
  1954.                      INTVAL (const_arg0));
  1955.           else if (const_arg1 != 0
  1956.                && GET_CODE (const_arg1) == CONST_INT
  1957.                && GET_CODE (XEXP (x, 0)) == PLUS
  1958.                && (CONSTANT_P (XEXP (XEXP (x, 0), 0))
  1959.                || CONSTANT_P (XEXP (XEXP (x, 0), 1))))
  1960.         new = plus_constant (XEXP (x, 0),
  1961.                      INTVAL (const_arg1));
  1962.         }
  1963.       else if (code == MINUS)
  1964.         {
  1965.           if (const_arg1 == const0_rtx)
  1966.         return XEXP (x, 0);
  1967.  
  1968.           if (XEXP (x, 0) == XEXP (x, 1)
  1969.           || (const_arg0 != 0 && const_arg0 == const_arg1))
  1970.         {
  1971.           /* We can't assume x-x is 0 with IEEE floating point.  */
  1972.           if (GET_MODE_CLASS (GET_MODE (x)) == MODE_INT)
  1973.             return const0_rtx;
  1974.         }
  1975.  
  1976.           if (const_arg0 != 0 && const_arg1 != 0
  1977.           /* Don't let a relocatable value get a negative coeff.  */
  1978.           && GET_CODE (const_arg1) == CONST_INT)
  1979.         new = plus_constant (const_arg0, - INTVAL (const_arg1));
  1980.         }
  1981.  
  1982.       /* PLUS and MULT can appear inside of a MEM.
  1983.          In such situations, a constant term must come second.  */
  1984.       else if (code == MULT || code == PLUS)
  1985.         if (copyflag && const_arg0 != 0)
  1986.           {
  1987.         if (! copied)
  1988.           x = gen_rtx (code, GET_MODE (x), XEXP (x, 0), XEXP (x, 1));
  1989.         XEXP (x, 0) = XEXP (x, 1);
  1990.         XEXP (x, 1) = const_arg0;
  1991.           }
  1992.  
  1993.       /* If integer truncation is being done with SUBREG,
  1994.          we can compute the result.  */
  1995.  
  1996.       else if (code == SUBREG)
  1997.         if (SUBREG_WORD (x) == 0
  1998.         && const_arg0 != 0
  1999.         && GET_CODE (const_arg0) == CONST_INT
  2000.         && GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))
  2001.         && (GET_MODE (x) == QImode || GET_MODE (x) == HImode
  2002.             || GET_MODE (x) == SImode))
  2003.           {
  2004.         arg0 = INTVAL (const_arg0);
  2005.         if (GET_MODE_BITSIZE (GET_MODE (x)) != 32)
  2006.           arg0 &= (1 << GET_MODE_BITSIZE (GET_MODE (x))) - 1;
  2007.         if (arg0 == INTVAL (const_arg0))
  2008.           new = const_arg0;
  2009.         else
  2010.           new = gen_rtx (CONST_INT, VOIDmode, arg0);
  2011.           }
  2012.  
  2013.       if (new != 0 && LEGITIMATE_CONSTANT_P (new))
  2014.         return new;
  2015.       return x;
  2016.     }
  2017.  
  2018.       /* Get the integer argument values in two forms:
  2019.      zero-extended in ARG0, ARG1 and sign-extended in ARG0S, ARG1S.  */
  2020.  
  2021.       arg0 = INTVAL (const_arg0);
  2022.       arg1 = INTVAL (const_arg1);
  2023.  
  2024.       if (width < HOST_BITS_PER_INT)
  2025.     {
  2026.       arg0 &= (1 << width) - 1;
  2027.       arg1 &= (1 << width) - 1;
  2028.  
  2029.       arg0s = arg0;
  2030.       if (arg0s & (1 << (width - 1)))
  2031.         arg0s |= ((-1) << width);
  2032.  
  2033.       arg1s = arg1;
  2034.       if (arg1s & (1 << (width - 1)))
  2035.         arg1s |= ((-1) << width);
  2036.     }
  2037.       else
  2038.     {
  2039.       arg0s = arg0;
  2040.       arg1s = arg1;
  2041.     }
  2042.  
  2043.       /* Compute the value of the arithmetic.  */
  2044.  
  2045.       switch (code)
  2046.     {
  2047.     case PLUS:
  2048.       val = arg0 + arg1;
  2049.       break;
  2050.  
  2051.     case MINUS:
  2052.       if (GET_MODE (x) == VOIDmode)
  2053.         /* Overflowless comparison:
  2054.            cannot represent an exact answer, so don't fold.
  2055.            This is used only to set the CC0,
  2056.            and fold_cc0 will take care of it.  */
  2057.         return x;
  2058.       val = arg0 - arg1;
  2059.       break;
  2060.  
  2061.     case MULT:
  2062.       val = arg0s * arg1s;
  2063.       break;
  2064.  
  2065.     case DIV:
  2066.       if (arg1s == 0)
  2067.         return x;
  2068.       val = arg0s / arg1s;
  2069.       break;
  2070.  
  2071.     case MOD:
  2072.       if (arg1s == 0)
  2073.         return x;
  2074.       val = arg0s % arg1s;
  2075.       break;
  2076.  
  2077.     case UMULT:
  2078.       val = (unsigned) arg0 * arg1;
  2079.       break;
  2080.  
  2081.     case UDIV:
  2082.       if (arg1 == 0)
  2083.         return x;
  2084.       val = (unsigned) arg0 / arg1;
  2085.       break;
  2086.  
  2087.     case UMOD:
  2088.       if (arg1 == 0)
  2089.         return x;
  2090.       val = (unsigned) arg0 % arg1;
  2091.       break;
  2092.  
  2093.     case AND:
  2094.       val = arg0 & arg1;
  2095.       break;
  2096.  
  2097.     case IOR:
  2098.       val = arg0 | arg1;
  2099.       break;
  2100.  
  2101.     case XOR:
  2102.       val = arg0 ^ arg1;
  2103.       break;
  2104.  
  2105.     case NE:
  2106.       val = arg0 != arg1;
  2107.       break;
  2108.  
  2109.     case EQ:
  2110.       val = arg0 == arg1;
  2111.       break;
  2112.  
  2113.     case LE:
  2114.       val = arg0s <= arg1s;
  2115.       break;
  2116.  
  2117.     case LT:
  2118.       val = arg0s < arg1s;
  2119.       break;
  2120.  
  2121.     case GE:
  2122.       val = arg0s >= arg1s;
  2123.       break;
  2124.  
  2125.     case GT:
  2126.       val = arg0s > arg1s;
  2127.       break;
  2128.  
  2129.     case LEU:
  2130.       val = ((unsigned) arg0) <= ((unsigned) arg1);
  2131.       break;
  2132.  
  2133.     case LTU:
  2134.       val = ((unsigned) arg0) < ((unsigned) arg1);
  2135.       break;
  2136.  
  2137.     case GEU:
  2138.       val = ((unsigned) arg0) >= ((unsigned) arg1);
  2139.       break;
  2140.  
  2141.     case GTU:
  2142.       val = ((unsigned) arg0) > ((unsigned) arg1);
  2143.       break;
  2144.  
  2145.     case LSHIFT:
  2146.       val = ((unsigned) arg0) << arg1;
  2147.       break;
  2148.  
  2149.     case ASHIFT:
  2150.       val = arg0s << arg1;
  2151.       break;
  2152.  
  2153.     case ROTATERT:
  2154.       arg1 = - arg1;
  2155.     case ROTATE:
  2156.       {
  2157.         int size = GET_MODE_SIZE (GET_MODE (x)) * BITS_PER_UNIT;
  2158.         if (arg1 > 0)
  2159.           {
  2160.         arg1 %= size;
  2161.         val = ((((unsigned) arg0) << arg1)
  2162.                | (((unsigned) arg0) >> (size - arg1)));
  2163.           }
  2164.         else if (arg1 < 0)
  2165.           {
  2166.         arg1 = (- arg1) % size;
  2167.         val = ((((unsigned) arg0) >> arg1)
  2168.                | (((unsigned) arg0) << (size - arg1)));
  2169.           }
  2170.         else
  2171.           val = arg0;
  2172.       }
  2173.       break;
  2174.  
  2175.     case LSHIFTRT:
  2176.       val = ((unsigned) arg0) >> arg1;
  2177.       break;
  2178.  
  2179.     case ASHIFTRT:
  2180.       val = arg0s >> arg1;
  2181.       break;
  2182.  
  2183.     default:
  2184.       return x;
  2185.     }
  2186.     }
  2187.   else if (code == IF_THEN_ELSE && const_arg0 != 0
  2188.        && GET_CODE (const_arg0) == CONST_INT)
  2189.     return XEXP (x, ((INTVAL (const_arg0) != 0) ? 1 : 2));
  2190.   else if (code == SIGN_EXTRACT || code == ZERO_EXTRACT)
  2191.     {
  2192.       if (const_arg0 != 0 && const_arg1 != 0 && const_arg2 != 0
  2193.       && GET_CODE (const_arg0) == CONST_INT
  2194.       && GET_CODE (const_arg1) == CONST_INT
  2195.       && GET_CODE (const_arg2) == CONST_INT)
  2196.     {
  2197.       /* Extracting a bit-field from a constant */
  2198.       val = INTVAL (const_arg0);
  2199. #ifdef BITS_BIG_ENDIAN
  2200.       val >>= (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
  2201.            - INTVAL (const_arg2) - INTVAL (const_arg1));
  2202. #else
  2203.       val >>= INTVAL (const_arg2);
  2204. #endif
  2205.       if (HOST_BITS_PER_INT != INTVAL (const_arg1))
  2206.         {
  2207.           /* First zero-extend.  */
  2208.           val &= (1 << INTVAL (const_arg1)) - 1;
  2209.           /* If desired, propagate sign bit.  */
  2210.           if (code == SIGN_EXTRACT
  2211.           && (val & (1 << (INTVAL (const_arg1) - 1))))
  2212.         val |= ~ (1 << INTVAL (const_arg1));
  2213.         }
  2214.     }
  2215.       else
  2216.     return x;
  2217.     }
  2218.   else
  2219.     return x;
  2220.  
  2221.   /* Clear the bits that don't belong in our mode,
  2222.      unless they and our sign bit are all one.
  2223.      So we get either a reasonable negative value or a reasonable
  2224.      unsigned value for this mode.  */
  2225.   if (width < HOST_BITS_PER_INT)
  2226.     {
  2227.       if ((val & ((-1) << (width - 1)))
  2228.       != ((-1) << (width - 1)))
  2229.     val &= (1 << width) - 1;
  2230.     }
  2231.  
  2232.   /* Now make the new constant.  */
  2233.   {
  2234.     rtx new = gen_rtx (CONST_INT, VOIDmode, val);
  2235.     return LEGITIMATE_CONSTANT_P (new) ? new : x;
  2236.   }
  2237. }
  2238.  
  2239. /* Given an expression X which is used to set CC0,
  2240.    return an integer recording (in the encoding used for prev_insn_cc0)
  2241.    how the condition codes would be set by that expression.
  2242.    Return 0 if the value is not constant
  2243.    or if there is any doubt what condition codes result from it.  */
  2244.  
  2245. static int
  2246. fold_cc0 (x)
  2247.      rtx x;
  2248. {
  2249.   if (GET_CODE (x) == MINUS && GET_MODE (x) == VOIDmode)
  2250.     {
  2251.       rtx y0 = fold_rtx (XEXP (x, 0), 0);
  2252.       rtx y1 = fold_rtx (XEXP (x, 1), 0);
  2253.       int u0, u1, s0, s1;
  2254.       enum machine_mode m;
  2255.  
  2256.       m = GET_MODE (y0);
  2257.       if (m == VOIDmode)
  2258.     m = GET_MODE (y1);
  2259.       if (m == VOIDmode)
  2260.     return 0;
  2261.  
  2262.       if (GET_CODE (y0) == REG)
  2263.     y0 = qty_const[reg_qty[REGNO (y0)]];
  2264.  
  2265.       if (y0 == 0 || GET_CODE (y0) != CONST_INT)
  2266.     return 0;
  2267.  
  2268.       if (GET_CODE (y1) == REG)
  2269.     y1 = qty_const[reg_qty[REGNO (y1)]];
  2270.  
  2271.       if (y1 == 0 || GET_CODE (y1) != CONST_INT)
  2272.     return 0;
  2273.  
  2274.       s0 = u0 = INTVAL (y0);
  2275.       s1 = u1 = INTVAL (y1);
  2276.  
  2277.       {
  2278.     int width = GET_MODE_BITSIZE (m);
  2279.     if (width < HOST_BITS_PER_INT)
  2280.       {
  2281.         s0 = u0 &= ~ ((-1) << width);
  2282.         s1 = u1 &= ~ ((-1) << width);
  2283.         if (u0 & (1 << (width - 1)))
  2284.           s0 |= ((-1) << width);
  2285.         if (u1 & (1 << (width - 1)))
  2286.           s1 |= ((-1) << width);
  2287.       }
  2288.       }
  2289.  
  2290.       return 0100 + ((s0 < s1 ? 7 : s0 > s1) << 3)
  2291.     + (((unsigned) u0 < (unsigned) u1) ? 7
  2292.        : ((unsigned) u0 > (unsigned) u1));
  2293.     }
  2294.   {
  2295.     rtx y0;
  2296.     int u0, s0;
  2297.     enum machine_mode m;
  2298.  
  2299.     y0 = fold_rtx (x, 0);
  2300.  
  2301.     m = GET_MODE (y0);
  2302.  
  2303.     if (GET_CODE (y0) == REG)
  2304.       y0 = qty_const[reg_qty[REGNO (y0)]];
  2305.  
  2306.     /* Register had no constant equivalent?  We can't do anything.  */
  2307.     if (y0 == 0)
  2308.       return 0;
  2309.  
  2310.     /* Value is frame-pointer plus a constant?  Or non-explicit constant?
  2311.        That isn't zero, but we don't know its sign.  */
  2312.     if (FIXED_BASE_PLUS_P (y0)
  2313.     || GET_CODE (y0) == SYMBOL_REF || GET_CODE (y0) == CONST)
  2314.       return 0300 + (1<<3) + 1;
  2315.  
  2316.     /* Otherwise, only integers enable us to optimize.  */
  2317.     if (GET_CODE (y0) != CONST_INT)
  2318.       return 0;
  2319.  
  2320.     s0 = u0 = INTVAL (y0);
  2321.     if (m != VOIDmode)
  2322.       {
  2323.     int width = GET_MODE_BITSIZE (m);
  2324.     if (width < HOST_BITS_PER_INT)
  2325.       {
  2326.         s0 = u0 &= ~ ((-1) << GET_MODE_BITSIZE (m));
  2327.         if (u0 & (1 << (GET_MODE_BITSIZE (m) - 1)))
  2328.           s0 |= ((-1) << GET_MODE_BITSIZE (m));
  2329.       }
  2330.       }
  2331.     return 0100 + ((s0 < 0 ? 7 : s0 > 0) << 3) + (u0 != 0);
  2332.   }
  2333. }
  2334.  
  2335. /* Attempt to prove that a loop will be executed >= 1 times,
  2336.    or prove it will be executed 0 times.
  2337.    If either can be proved, delete some of the code.  */
  2338.  
  2339. static void
  2340. predecide_loop_entry (insn)
  2341.      register rtx insn;
  2342. {
  2343.   register rtx jump = NEXT_INSN (insn);
  2344.   register rtx p;
  2345.   register rtx loop_top_label = NEXT_INSN (jump);
  2346.   enum { UNK, DELETE_LOOP, DELETE_JUMP } disposition = UNK;
  2347.   int count = 0;
  2348.  
  2349.   /* Find the label at the top of the loop.  */
  2350.   while (GET_CODE (loop_top_label) == BARRIER
  2351.      || GET_CODE (loop_top_label) == NOTE)
  2352.     loop_top_label = NEXT_INSN (loop_top_label);
  2353.   if (GET_CODE (loop_top_label) != CODE_LABEL)
  2354.     abort ();
  2355.  
  2356.   /* Find the label at which the loop is entered.  */
  2357.   p = XEXP (SET_SRC (PATTERN (jump)), 0);
  2358.   if (GET_CODE (p) != CODE_LABEL)
  2359.     abort ();
  2360.  
  2361.   /* Trace the flow of control through the end test,
  2362.      propagating constants, to see if result is determined.  */
  2363.   prev_insn_cc0 = 0;
  2364.   /* Avoid infinite loop if we find a cycle of jumps.  */
  2365.   while (count < 10)
  2366.     {
  2367.       /* At end of function?  Means rtl is inconsistent,
  2368.      but this can happen when stmt.c gets confused
  2369.      by a syntax error.  */
  2370.       if (p == 0)
  2371.     break;
  2372.       /* Arriving at end of loop means endtest will drop out.  */
  2373.       if (GET_CODE (p) == NOTE
  2374.       && NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
  2375.     {
  2376.       disposition = DELETE_LOOP;
  2377.       break;
  2378.     }
  2379.       else if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == NOTE)
  2380.     ;
  2381.       /* We only know how to handle two kinds of insns:
  2382.      conditional jumps, and those that set the condition codes. */
  2383.       else if (GET_CODE (p) == INSN && GET_CODE (PATTERN (p)) == SET
  2384.            && SET_DEST (PATTERN (p)) == cc0_rtx)
  2385.     {
  2386.       prev_insn_cc0 = fold_cc0 (copy_rtx (SET_SRC (PATTERN (p))));
  2387.     }
  2388.       else if (GET_CODE (p) == JUMP_INSN
  2389.            && GET_CODE (PATTERN (p)) == SET
  2390.            && SET_DEST (PATTERN (p)) == pc_rtx)
  2391.     {
  2392.       register rtx target
  2393.         = fold_rtx (SET_SRC (PATTERN (p)), 1);
  2394.       if (GET_CODE (target) == LABEL_REF)
  2395.         p = XEXP (target, 0);
  2396.       else if (target != pc_rtx)
  2397.         /* If destination of jump is not fixed, give up.  */
  2398.         break;
  2399.       count++;
  2400.     }
  2401.       /* Any other kind of insn means we don't know
  2402.      what result the test will have.  */
  2403.       else
  2404.     break;
  2405.  
  2406.       /* Arriving at top of loop means we can drop straight in.
  2407.      Check here because we can arrive only via a jump insn
  2408.      which would have changed P above.  */
  2409.       if (p == loop_top_label)
  2410.     {
  2411.       disposition = DELETE_JUMP;
  2412.       break;
  2413.     }
  2414.       /* We went past one insn; consider the next.  */
  2415.       p = NEXT_INSN (p);
  2416.     }
  2417.   if (disposition == DELETE_JUMP)
  2418.     {
  2419.       /* We know the loop test will succeed the first time,
  2420.      so delete the jump to the test; drop right into loop.
  2421.      Note that one call to delete_insn gets the BARRIER as well.  */
  2422.       delete_insn (jump);
  2423.     }
  2424.   if (disposition == DELETE_LOOP)
  2425.     {
  2426.       /* We know the endtest will fail and drop right out of the loop,
  2427.      but it isn't safe to delete the loop here.
  2428.      There could be jumps into it from outside.
  2429.      So make the entry-jump jump around the loop.
  2430.      This will cause find_basic_blocks to delete it if appropriate.  */
  2431.       register rtx label = gen_label_rtx ();
  2432.       emit_label_after (label, p);
  2433.       redirect_jump (jump, label);
  2434.     }
  2435. }
  2436.  
  2437. /* CSE processing for one instruction.
  2438.    First simplify sources and addresses of all assignments
  2439.    in the instruction, using previously-computed equivalents values.
  2440.    Then install the new sources and destinations in the table
  2441.    of available values.  */
  2442.  
  2443. static rtx set[MAX_SETS_PER_INSN];
  2444. static struct table_elt *src_elt[MAX_SETS_PER_INSN];
  2445. static int src_hash_code[MAX_SETS_PER_INSN];
  2446. static int dest_hash_code[MAX_SETS_PER_INSN];
  2447. static char src_in_memory[MAX_SETS_PER_INSN];
  2448. static char src_in_struct[MAX_SETS_PER_INSN];
  2449. static rtx inner_dest[MAX_SETS_PER_INSN];
  2450. static char src_volatile[MAX_SETS_PER_INSN];
  2451.  
  2452. static void
  2453. cse_insn (insn)
  2454.      rtx insn;
  2455. {
  2456.   register rtx x = PATTERN (insn);
  2457.   register int i;
  2458.   register int n_sets = 0;
  2459.  
  2460.   /* Records what this insn does to set CC0,
  2461.      using same encoding used for prev_insn_cc0.  */
  2462.   int this_insn_cc0 = 0;
  2463.   struct write_data writes_memory;
  2464.   static struct write_data init = {0, 0, 0};
  2465.  
  2466.   rtx src_eqv = 0;
  2467.   struct table_elt *src_eqv_elt = 0;
  2468.   int src_eqv_in_memory;
  2469.   int src_eqv_in_struct;
  2470.   int src_eqv_volatile = 0;
  2471.   int src_eqv_hash_code;
  2472.  
  2473.   this_insn = insn;
  2474.   writes_memory = init;
  2475.  
  2476.   /* Find all the SETs and CLOBBERs in this instruction.
  2477.      Record all the SETs in the array `set' and count them.
  2478.      Also determine whether there is a CLOBBER that invalidates
  2479.      all memory references, or all references at varying addresses.  */
  2480.  
  2481.   if (GET_CODE (x) == SET)
  2482.     {
  2483.       rtx tem;
  2484.       n_sets = 1;
  2485.       set[0] = x;
  2486.  
  2487.       if (REG_NOTES (insn) != 0)
  2488.     {
  2489.       /* Store the equivalent value (re REG_EQUAL or REG_EQUIV) in SRC_EQV.  */
  2490.       tem = find_reg_note (insn, REG_EQUIV, 0);
  2491.       if (tem == 0)
  2492.         tem = find_reg_note (insn, REG_EQUAL, 0);
  2493.       if (tem) src_eqv = XEXP (tem, 0);
  2494.  
  2495.       /* Ignore the REG_EQUAL or REG_EQUIV note if its contents
  2496.          are the same as the source.  */
  2497.       if (src_eqv && rtx_equal_p (src_eqv, SET_SRC (x)))
  2498.         src_eqv = 0;
  2499.     }
  2500.  
  2501.       /* Return now for unconditional jumps.
  2502.      They never need cse processing, so this does not hurt.
  2503.      The reason is not efficiency but rather
  2504.      so that we can test at the end for instructions
  2505.      that have been simplified to unconditional jumps
  2506.      and not be misled by unchanged instructions
  2507.      that were unconditional jumps to begin with.  */
  2508.       if (SET_DEST (x) == pc_rtx
  2509.       && GET_CODE (SET_SRC (x)) == LABEL_REF)
  2510.     return;
  2511.  
  2512.       /* Return now for call-insns, (set (reg 0) (call ...)).
  2513.      The hard function value register is used only once, to copy to
  2514.      someplace else, so it isn't worth cse'ing (and on 80386 is unsafe)! */
  2515.       if (GET_CODE (SET_SRC (x)) == CALL)
  2516.     {
  2517.       canon_reg (SET_SRC (x));
  2518.       return;
  2519.     }
  2520.     }
  2521.   else if (GET_CODE (x) == PARALLEL)
  2522.     {
  2523.       register int lim = XVECLEN (x, 0);
  2524.       for (i = 0; i < lim; i++)
  2525.     {
  2526.       register rtx y = XVECEXP (x, 0, i);
  2527.       if (GET_CODE (y) == SET)
  2528.         set[n_sets++] = y;
  2529.       else if (GET_CODE (y) == CLOBBER)
  2530.         note_mem_written (XEXP (y, 0), &writes_memory);
  2531.       else if (GET_CODE (y) == CALL)
  2532.         canon_reg (y);
  2533.     }
  2534.     }
  2535.   else if (GET_CODE (x) == CLOBBER)
  2536.     note_mem_written (XEXP (x, 0), &writes_memory);
  2537.   else if (GET_CODE (x) == CALL)
  2538.     canon_reg (x);
  2539.  
  2540.   if (n_sets == 0)
  2541.     {
  2542.       invalidate_from_clobbers (&writes_memory, x);
  2543.       return;
  2544.     }
  2545.  
  2546.   /* Canonicalize sources and addresses of destinations.
  2547.      set src_elt[i] to the class each source belongs to.
  2548.      Detect assignments from or to volatile things
  2549.      and set set[i] to zero so they will be ignored
  2550.      in the rest of this function.
  2551.  
  2552.      Nothing in this loop changes the hash table or the register chains.  */
  2553.  
  2554.   for (i = 0; i < n_sets; i++)
  2555.     {
  2556.       register rtx src, dest;
  2557.       register struct table_elt *elt;
  2558.       enum machine_mode mode;
  2559.  
  2560.       dest = SET_DEST (set[i]);
  2561.       src = SET_SRC (set[i]);
  2562.  
  2563.       /* If SRC is a constant that has no machine mode,
  2564.      hash it with the destination's machine mode.
  2565.      This way we can keep different modes separate.  */
  2566.  
  2567.       mode = GET_MODE (src) == VOIDmode ? GET_MODE (dest) : GET_MODE (src);
  2568.  
  2569.       /* Replace each registers in SRC with oldest equivalent register,
  2570.      but if DEST is a register do not replace it if it appears in SRC.  */
  2571.  
  2572.       if (GET_CODE (dest) == REG)
  2573.     {
  2574.       int tem = reg_qty[REGNO (dest)];
  2575.       reg_qty[REGNO (dest)] = REGNO (dest);
  2576.       src = canon_reg (src);
  2577.  
  2578.       if (src_eqv)
  2579.         src_eqv = canon_reg (src_eqv);
  2580.  
  2581.       reg_qty[REGNO (dest)] = tem;
  2582.     }
  2583.       else
  2584.     {
  2585.       src = canon_reg (src);
  2586.  
  2587.       if (src_eqv)
  2588.         src_eqv = canon_reg (src_eqv);
  2589.     }
  2590.  
  2591.       if (src_eqv)
  2592.     {
  2593.       enum machine_mode eqvmode = mode;
  2594.       if (GET_CODE (dest) == STRICT_LOW_PART)
  2595.         eqvmode = GET_MODE (SUBREG_REG (XEXP (dest, 0)));
  2596.       do_not_record = 0;
  2597.       hash_arg_in_memory = 0;
  2598.       hash_arg_in_struct = 0;
  2599.       src_eqv = fold_rtx (src_eqv, 0);
  2600.       src_eqv_hash_code = HASH (src_eqv, eqvmode);
  2601.  
  2602.       /* Replace the src_eqv with its cheapest equivalent.  */
  2603.  
  2604.       if (!do_not_record)
  2605.         {
  2606.           elt = lookup (src_eqv, src_eqv_hash_code, eqvmode);
  2607.           if (elt && elt != elt->first_same_value)
  2608.         {
  2609.           elt = elt->first_same_value;
  2610.           /* Find the cheapest one that is still valid.  */
  2611.           while ((GET_CODE (elt->exp) != REG
  2612.               && !exp_equiv_p (elt->exp, elt->exp, 1))
  2613.              || elt->equivalence_only)
  2614.             elt = elt->next_same_value;
  2615.           src_eqv = copy_rtx (elt->exp);
  2616.           hash_arg_in_memory = 0;
  2617.           hash_arg_in_struct = 0;
  2618.           src_eqv_hash_code = HASH (src_eqv, elt->mode);
  2619.         }
  2620.           src_eqv_elt = elt;
  2621.         }
  2622.       else
  2623.         src_eqv = 0;
  2624.  
  2625.       src_eqv_in_memory = hash_arg_in_memory;
  2626.       src_eqv_in_struct = hash_arg_in_struct;
  2627.     }
  2628.  
  2629.       /* Compute SRC's hash code, and also notice if it
  2630.      should not be recorded at all.  In that case,
  2631.      prevent any further processing of this assignment.  */
  2632.       do_not_record = 0;
  2633.       hash_arg_in_memory = 0;
  2634.       hash_arg_in_struct = 0;
  2635.       src = fold_rtx (src, 0);
  2636.       /* If we have (NOT Y), see if Y is known to be (NOT Z).
  2637.      If so, (NOT Y) simplifies to Z.  */
  2638.       if (GET_CODE (src) == NOT || GET_CODE (src) == NEG)
  2639.     {
  2640.       rtx y = lookup_as_function (XEXP (src, 0), GET_CODE (src));
  2641.       if (y != 0)
  2642.         src = y;
  2643.     }
  2644.  
  2645.       /* If storing a constant value in a register that
  2646.      previously held the constant value 0,
  2647.      record this fact with a REG_WAS_0 note on this insn.  */
  2648.       if (GET_CODE (src) == CONST_INT
  2649.       && GET_CODE (dest) == REG
  2650.       && qty_const[reg_qty[REGNO (dest)]] == const0_rtx)
  2651.     REG_NOTES (insn) = gen_rtx (INSN_LIST, REG_WAS_0,
  2652.                     qty_const_insn[reg_qty[REGNO (dest)]],
  2653.                     REG_NOTES (insn));
  2654.  
  2655.       src_hash_code[i] = HASH (src, mode);
  2656.  
  2657.       src_volatile[i] = do_not_record;
  2658.  
  2659. #if 0
  2660.       /* This code caused multiple hash-table entries
  2661.      to be created for registers.  Invalidation
  2662.      would only get one, leaving others that didn't belong.
  2663.      I don't know what good this ever did.  */
  2664.       if (GET_CODE (src) == REG)
  2665.     {
  2666.       src_in_memory[i] = 0;
  2667.       src_elt[i] = 0;
  2668.     }
  2669.       else ...;
  2670. #endif
  2671.       /* If source is a perverse subreg (such as QI treated as an SI),
  2672.      treat it as volatile.  It may do the work of an SI in one context
  2673.      where the extra bits are not being used, but cannot replace an SI
  2674.      in general.  */
  2675.       if (GET_CODE (src) == SUBREG
  2676.       && (GET_MODE_SIZE (GET_MODE (src))
  2677.           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))))
  2678.     src_volatile[i] = 1;
  2679.       else if (!src_volatile[i])
  2680.     {
  2681.       /* Replace the source with its cheapest equivalent.  */
  2682.  
  2683.       elt = lookup (src, src_hash_code[i], mode);
  2684.       if (elt && elt != elt->first_same_value)
  2685.         {
  2686.           elt = elt->first_same_value;
  2687.           /* Find the cheapest one that is still valid.  */
  2688.           while ((GET_CODE (elt->exp) != REG
  2689.               && !exp_equiv_p (elt->exp, elt->exp, 1))
  2690.              || elt->equivalence_only)
  2691.         elt = elt->next_same_value;
  2692.           src = copy_rtx (elt->exp);
  2693.           hash_arg_in_memory = 0;
  2694.           hash_arg_in_struct = 0;
  2695.           src_hash_code[i] = HASH (src, elt->mode);
  2696.         }
  2697.  
  2698.       /* If ELT is a constant, is there a register
  2699.          linearly related to it?  If so, replace it
  2700.          with the sum of that register plus an offset.  */
  2701.  
  2702.       if (GET_CODE (src) == CONST && n_sets == 1
  2703.           && SET_DEST (set[i]) != cc0_rtx)
  2704.         {
  2705.           rtx newsrc = use_related_value (src, elt);
  2706.           if (newsrc == 0 && src_eqv != 0)
  2707.         newsrc = use_related_value (src_eqv, src_eqv_elt);
  2708.           if (newsrc)
  2709.         {
  2710.           rtx oldsrc = src;
  2711.           src = newsrc;
  2712.           hash_arg_in_memory = 0;
  2713.           hash_arg_in_struct = 0;
  2714.           src_hash_code[i] = HASH (src, GET_MODE (src));
  2715.           /* The new expression for the SRC has the same value
  2716.              as the previous one; so if the previous one is in
  2717.              the hash table, put the new one in as equivalent.  */
  2718.           if (elt != 0)
  2719.             elt = insert (src, elt->first_same_value, src_hash_code[i],
  2720.                   elt->mode);
  2721.           else
  2722.             {
  2723.               /* Maybe the new expression is in the table already.  */
  2724.               elt = lookup (src, src_hash_code[i], mode);
  2725.               /* And maybe a register contains the same value.  */
  2726.               if (elt && elt != elt->first_same_value)
  2727.             {
  2728.               elt = elt->first_same_value;
  2729.               /* Find the cheapest one that is still valid.  */
  2730.               while ((GET_CODE (elt->exp) != REG
  2731.                   && !exp_equiv_p (elt->exp, elt->exp, 1))
  2732.                  || elt->equivalence_only)
  2733.                 elt = elt->next_same_value;
  2734.               src = copy_rtx (elt->exp);
  2735.               hash_arg_in_memory = 0;
  2736.               hash_arg_in_struct = 0;
  2737.               src_hash_code[i] = HASH (src, elt->mode);
  2738.             }
  2739.             }
  2740.  
  2741.           /* This would normally be inhibited by the REG_EQUIV
  2742.              note we are about to make.  */
  2743. #if 0
  2744.           /* Deleted because the inhibition was deleted.  */
  2745.           SET_SRC (set[i]) = src;
  2746. #endif
  2747.  
  2748.           /* Record the actual constant value in a REG_EQUIV note.  */
  2749.           if (GET_CODE (SET_DEST (set[i])) == REG)
  2750.             REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_EQUIV,
  2751.                         oldsrc, 0);
  2752.         }
  2753.         }
  2754.  
  2755.       src_elt[i] = elt;
  2756.       src_in_memory[i] = hash_arg_in_memory;
  2757.       src_in_struct[i] = hash_arg_in_struct;
  2758.     }
  2759.  
  2760.       /* Either canon_reg or the copy_rtx may have changed this.  */
  2761.       /* Note it is not safe to replace the sources if there
  2762.      is more than one set.  We could get an insn
  2763.      [(set (reg) (reg)) (set (reg) (reg))], which is probably
  2764.      not in the machine description.
  2765.      This case we could handle by breaking into several insns.
  2766.      Cases of partial substitution cannot win at all.  */
  2767.       /* Also, if this insn is setting a "constant" register,
  2768.      we may not replace the value that is given to it.  */
  2769.       if (n_sets == 1)
  2770. #if 0
  2771.     /* Now that the REG_EQUIV contains the constant instead of the reg,
  2772.        it should be ok to modify the insn's actual source.  */
  2773.     if (REG_NOTES (insn) == 0
  2774.         || REG_NOTE_KIND (REG_NOTES (insn)) != REG_EQUIV)
  2775. #endif
  2776.       SET_SRC (set[0]) = src;
  2777.  
  2778.       do_not_record = 0;
  2779.  
  2780.       /* Look within any SIGN_EXTRACT or ZERO_EXTRACT
  2781.      to the MEM or REG within it.  */
  2782.       while (1)
  2783.     {
  2784.       if (GET_CODE (dest) == SIGN_EXTRACT
  2785.           || GET_CODE (dest) == ZERO_EXTRACT)
  2786.         {
  2787.           XEXP (dest, 1) = canon_reg (XEXP (dest, 1));
  2788.           XEXP (dest, 2) = canon_reg (XEXP (dest, 2));
  2789.           dest = XEXP (dest, 0);
  2790.         }
  2791.       else if (GET_CODE (dest) == SUBREG
  2792.            || GET_CODE (dest) == STRICT_LOW_PART)
  2793.         dest = XEXP (dest, 0);
  2794.       else
  2795.         break;
  2796.     }
  2797.  
  2798.       inner_dest[i] = dest;
  2799.  
  2800.       /* If storing into memory, do cse on the memory address.
  2801.      Also compute the hash code of the destination now,
  2802.      before the effects of this instruction are recorded,
  2803.      since the register values used in the address computation
  2804.      are those before this instruction.  */
  2805.       if (GET_CODE (dest) == MEM)
  2806.     {
  2807.       register rtx addr;
  2808.       register int hash;
  2809.  
  2810.       canon_reg (dest);
  2811.       dest = fold_rtx (dest, 0);
  2812.       addr = XEXP (dest, 0);
  2813.  
  2814.       /* Pushing or popping does not invalidate anything.  */
  2815.       if ((GET_CODE (addr) == PRE_DEC || GET_CODE (addr) == PRE_INC
  2816.            || GET_CODE (addr) == POST_DEC || GET_CODE (addr) == POST_INC)
  2817.           && GET_CODE (XEXP (addr, 0)) == REG
  2818.           && REGNO (XEXP (addr, 0)) == STACK_POINTER_REGNUM)
  2819.         ;
  2820.       else
  2821.         /* Otherwise, decide whether we invalidate
  2822.            everything in memory, or just things at non-fixed places.
  2823.            Writing a large aggregate must invalidate everything
  2824.            because we don't know how long it is.  */
  2825.         note_mem_written (dest, &writes_memory);
  2826.  
  2827.       /* Do not try to replace addresses of local and argument slots.
  2828.          The MEM expressions for args and non-register local variables
  2829.          are made only once and inserted in many instructions,
  2830.          as well as being used to control symbol table output.
  2831.          It is not safe to clobber them.  It also doesn't do any good!  */
  2832.       if ((GET_CODE (addr) == PLUS
  2833.            && GET_CODE (XEXP (addr, 0)) == REG
  2834.            && GET_CODE (XEXP (addr, 1)) == CONST_INT
  2835.            && (hash = REGNO (XEXP (addr, 0)),
  2836.            hash == FRAME_POINTER_REGNUM || hash == ARG_POINTER_REGNUM))
  2837.           || (GET_CODE (addr) == REG
  2838.           && (hash = REGNO (addr),
  2839.               hash == FRAME_POINTER_REGNUM || hash == ARG_POINTER_REGNUM)))
  2840.         dest_hash_code[i] = ((int)MEM + canon_hash (addr)) % NBUCKETS;
  2841.       else
  2842.         {
  2843.           /* Look for a simpler equivalent for the destination address.  */
  2844.           hash = HASH (addr, Pmode);
  2845.           if (! do_not_record)
  2846.         {
  2847.           elt = lookup (addr, hash, Pmode);
  2848.           dest_hash_code[i] = ((int) MEM + hash) % NBUCKETS;
  2849.  
  2850.           if (elt && elt != elt->first_same_value)
  2851.             {
  2852.               elt = elt->first_same_value;
  2853.               /* Find the cheapest one that is still valid.  */
  2854.               while ((GET_CODE (elt->exp) != REG
  2855.                   && !exp_equiv_p (elt->exp, elt->exp, 1))
  2856.                  || elt->equivalence_only)
  2857.             elt = elt->next_same_value;
  2858.  
  2859.               addr = copy_rtx (elt->exp);
  2860.               /* Create a new MEM rtx, in case the old one
  2861.              is shared somewhere else.  */
  2862.               dest = gen_rtx (MEM, GET_MODE (dest), addr);
  2863.               dest->volatil = inner_dest[i]->volatil;
  2864.               SET_DEST (set[i]) = dest;
  2865.               inner_dest[i] = dest;
  2866.             }
  2867.         }
  2868.         }
  2869.     }
  2870.  
  2871.       /* Don't enter a bit-field in the hash table
  2872.      because the value in it after the store
  2873.      may not equal what was stored, due to truncation.  */
  2874.  
  2875.       if (GET_CODE (SET_DEST (set[i])) == ZERO_EXTRACT
  2876.       || GET_CODE (SET_DEST (set[i])) == SIGN_EXTRACT)
  2877.     src_volatile[i] = 1, src_eqv = 0;
  2878.  
  2879.       /* No further processing for this assignment
  2880.      if destination is volatile.  */
  2881.  
  2882.       else if (do_not_record
  2883.            || (GET_CODE (dest) == REG 
  2884.            ? REGNO (dest) == STACK_POINTER_REGNUM
  2885.            : GET_CODE (dest) != MEM))
  2886.     set[i] = 0;
  2887.  
  2888.       if (set[i] != 0 && dest != SET_DEST (set[i]))
  2889.     dest_hash_code[i] = HASH (SET_DEST (set[i]), mode);
  2890.  
  2891.       if (dest == cc0_rtx
  2892.       && (GET_CODE (src) == MINUS
  2893.           || CONSTANT_P (src)
  2894.           || GET_CODE (src) == REG))
  2895.     this_insn_cc0 = fold_cc0 (src);
  2896.     }
  2897.  
  2898.   /* Now enter all non-volatile source expressions in the hash table
  2899.      if they are not already present.
  2900.      Record in src_elt the heads of their equivalence classes.
  2901.      This way we can insert the corresponding destinations into
  2902.      the same classes even if the actual sources are no longer in them
  2903.      (having been invalidated).  */
  2904.  
  2905.   if (src_eqv && src_eqv_elt == 0 && set[0] != 0)
  2906.     {
  2907.       register struct table_elt *elt;
  2908.       rtx dest = SET_DEST (set[0]);
  2909.       enum machine_mode eqvmode = GET_MODE (dest);
  2910.  
  2911.       if (GET_CODE (dest) == STRICT_LOW_PART)
  2912.     eqvmode = GET_MODE (SUBREG_REG (XEXP (dest, 0)));
  2913.       if (insert_regs (src_eqv, 0, 0))
  2914.     src_eqv_hash_code = HASH (src_eqv, eqvmode);
  2915.       elt = insert (src_eqv, 0, src_eqv_hash_code, eqvmode);
  2916.       elt->in_memory = src_eqv_in_memory;
  2917.       elt->in_struct = src_eqv_in_struct;
  2918.       elt->equivalence_only = 1;
  2919.       src_eqv_elt = elt->first_same_value;
  2920.     }
  2921.  
  2922.   for (i = 0; i < n_sets; i++)
  2923.     if (set[i] && ! src_volatile[i])
  2924.       {
  2925.     if (GET_CODE (SET_DEST (set[i])) == STRICT_LOW_PART)
  2926.       {
  2927.         /* REG_EQUAL in setting a STRICT_LOW_PART
  2928.            gives an equivalent for the entire destination register,
  2929.            not just for the subreg being stored in now.
  2930.            This is a more interesting equivalent, so we arrange later
  2931.            to treat the entire reg as the destination.  */
  2932.         src_elt[i] = src_eqv_elt;
  2933.         src_hash_code[i] = src_eqv_hash_code;
  2934.       }
  2935.     else if (src_elt[i] == 0)
  2936.       {
  2937.         register rtx src = SET_SRC (set[i]);
  2938.         register rtx dest = SET_DEST (set[i]);
  2939.         register struct table_elt *elt;
  2940.         enum machine_mode mode
  2941.           = GET_MODE (src) == VOIDmode ? GET_MODE (dest) : GET_MODE (src);
  2942.  
  2943.         /* Note that these insert_regs calls cannot remove
  2944.            any of the src_elt's, because they would have failed to match
  2945.            if not still valid.  */
  2946.         if (insert_regs (src, 0, 0))
  2947.           src_hash_code[i] = HASH (src, mode);
  2948.         elt = insert (src, src_eqv_elt, src_hash_code[i], mode);
  2949.         elt->in_memory = src_in_memory[i];
  2950.         elt->in_struct = src_in_struct[i];
  2951.         src_elt[i] = elt->first_same_value;
  2952.       }
  2953.       }
  2954.  
  2955.   invalidate_from_clobbers (&writes_memory, x);
  2956.  
  2957.   /* Now invalidate everything set by this instruction.
  2958.      If a SUBREG or other funny destination is being set,
  2959.      set[i] is still nonzero, so here we invalidate the reg
  2960.      a part of which is being set.  */
  2961.  
  2962.   for (i = 0; i < n_sets; i++)
  2963.     if (set[i])
  2964.       {
  2965.     register rtx dest = inner_dest[i];
  2966.  
  2967.     /* Needed for registers to remove the register from its
  2968.        previous quantity's chain.
  2969.        Needed for memory if this is a nonvarying address, unless
  2970.        we have just done an invalidate_memory that covers even those.  */
  2971.     if (GET_CODE (dest) == REG || GET_CODE (dest) == SUBREG
  2972.         || (! writes_memory.all && ! cse_rtx_addr_varies_p (dest)))
  2973.       invalidate (dest);
  2974.       }
  2975.  
  2976.   /* Make sure registers mentioned in destinations
  2977.      are safe for use in an expression to be inserted.
  2978.      This removes from the hash table
  2979.      any invalid entry that refers to one of these registers.  */
  2980.  
  2981.   for (i = 0; i < n_sets; i++)
  2982.     if (set[i] && GET_CODE (SET_DEST (set[i])) != REG)
  2983.       mention_regs (SET_DEST (set[i]));
  2984.  
  2985.   /* We may have just removed some of the src_elt's from the hash table.
  2986.      So replace each one with the current head of the same class.  */
  2987.  
  2988.   for (i = 0; i < n_sets; i++)
  2989.     if (set[i])
  2990.       {
  2991.     /* If the source is volatile, its destination goes in
  2992.        a class of its own.  */
  2993.     if (src_volatile[i])
  2994.       src_elt[i] = 0;
  2995.  
  2996.     if (src_elt[i] && src_elt[i]->first_same_value == 0)
  2997.       /* If elt was removed, find current head of same class,
  2998.          or 0 if nothing remains of that class.  */
  2999.       {
  3000.         register struct table_elt *elt = src_elt[i];
  3001.  
  3002.         while (elt && elt->first_same_value == 0)
  3003.           elt = elt->next_same_value;
  3004.         src_elt[i] = elt ? elt->first_same_value : 0;
  3005.       }
  3006.       }
  3007.  
  3008.   /* Now insert the destinations into their equivalence classes.  */
  3009.  
  3010.   for (i = 0; i < n_sets; i++)
  3011.     if (set[i])
  3012.       {
  3013.     register rtx dest = SET_DEST (set[i]);
  3014.     register struct table_elt *elt;
  3015.  
  3016.     /* STRICT_LOW_PART isn't part of the value BEING set,
  3017.        and neither is the SUBREG inside it.
  3018.        Note that in this case SRC_ELT[I] is really SRC_EQV_ELT.  */
  3019.     if (GET_CODE (dest) == STRICT_LOW_PART)
  3020.       dest = SUBREG_REG (XEXP (dest, 0));
  3021.  
  3022.     if (GET_CODE (dest) == REG)
  3023.       /* Registers must also be inserted into chains for quantities.  */
  3024.       if (insert_regs (dest, src_elt[i], 1))
  3025.         /* If `insert_regs' changes something, the hash code must be
  3026.            recalculated.  */
  3027.         dest_hash_code[i] = HASHREG (dest);
  3028.  
  3029.     if (GET_CODE (dest) == SUBREG)
  3030.       /* Registers must also be inserted into chains for quantities.  */
  3031.       if (insert_regs (dest, src_elt[i], 1))
  3032.         /* If `insert_regs' changes something, the hash code must be
  3033.            recalculated.  */
  3034.         dest_hash_code[i] = canon_hash (dest) % NBUCKETS;
  3035.  
  3036.     elt = insert (dest, src_elt[i], dest_hash_code[i], GET_MODE (dest));
  3037.     elt->in_memory = GET_CODE (inner_dest[i]) == MEM;
  3038.     if (elt->in_memory)
  3039.       {
  3040.         elt->in_struct = (inner_dest[i]->in_struct
  3041.                   || inner_dest[i] != SET_DEST (set[i]));
  3042.       }
  3043.       }
  3044.  
  3045.   /* Special handling for (set REG0 REG1)
  3046.      where REG0 is the "cheapest", cheaper than REG1.
  3047.      After cse, REG1 will probably not be used in the sequel, 
  3048.      so (if easily done) change this insn to (set REG1 REG0) and
  3049.      replace REG1 with REG0 in the previous insn that computed their value.
  3050.      Then REG1 will become a dead store and won't cloud the situation
  3051.      for later optimizations.  */
  3052.   if (n_sets == 1 && set[0] && GET_CODE (SET_DEST (set[0])) == REG
  3053.       && GET_CODE (SET_SRC (set[0])) == REG
  3054.       && rtx_equal_p (canon_reg (SET_SRC (set[0])), SET_DEST (set[0])))
  3055.     {
  3056.       rtx prev = PREV_INSN (insn);
  3057.       while (prev && GET_CODE (prev) == NOTE)
  3058.     prev = PREV_INSN (prev);
  3059.  
  3060.       if (prev && GET_CODE (prev) == INSN && GET_CODE (PATTERN (prev)) == SET
  3061.       && SET_DEST (PATTERN (prev)) == SET_SRC (set[0]))
  3062.     {
  3063.       rtx dest = SET_DEST (set[0]);
  3064.       SET_DEST (PATTERN (prev)) = dest;
  3065.       SET_DEST (set[0]) = SET_SRC (set[0]);
  3066.       SET_SRC (set[0]) = dest;
  3067.     }
  3068.     }
  3069.  
  3070.   /* Did this insn become an unconditional branch or become a no-op?  */
  3071.   if (GET_CODE (insn) == JUMP_INSN
  3072.       && GET_CODE (x) == SET
  3073.       && SET_DEST (x) == pc_rtx)
  3074.     {
  3075.       if (SET_SRC (x) == pc_rtx)
  3076.     {
  3077.       PUT_CODE (insn, NOTE);
  3078.       NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
  3079.       NOTE_SOURCE_FILE (insn) = 0;
  3080.       cse_jumps_altered = 1;
  3081.       /* If previous insn just set CC0 for us, delete it too.  */
  3082.       if (prev_insn_cc0 != 0)
  3083.         {
  3084.           PUT_CODE (prev_insn, NOTE);
  3085.           NOTE_LINE_NUMBER (prev_insn) = NOTE_INSN_DELETED;
  3086.           NOTE_SOURCE_FILE (prev_insn) = 0;
  3087.         }
  3088.     }
  3089.       else if (GET_CODE (SET_SRC (x)) == LABEL_REF)
  3090.     {
  3091.       emit_barrier_after (insn);
  3092.       cse_jumps_altered = 1;
  3093.       /* If previous insn just set CC0 for us, delete it too.  */
  3094.       if (prev_insn_cc0 != 0)
  3095.         {
  3096.           PUT_CODE (prev_insn, NOTE);
  3097.           NOTE_LINE_NUMBER (prev_insn) = NOTE_INSN_DELETED;
  3098.           NOTE_SOURCE_FILE (prev_insn) = 0;
  3099.         }
  3100.     }
  3101.     }
  3102.  
  3103.   /* If this insn used to store a value based on CC0 but now value is constant,
  3104.      and the previous insn just set CC0 for us, delete previous insn.
  3105.      Here we use the fact that nothing expects CC0 to be valid over an insn,
  3106.      which is true until the final pass.  */
  3107.   if (GET_CODE (x) == SET && prev_insn_cc0
  3108.       && CONSTANT_P (SET_SRC (x)))
  3109.     {
  3110.       PUT_CODE (prev_insn, NOTE);
  3111.       NOTE_LINE_NUMBER (prev_insn) = NOTE_INSN_DELETED;
  3112.       NOTE_SOURCE_FILE (prev_insn) = 0;
  3113.     }
  3114.  
  3115.   prev_insn_cc0 = this_insn_cc0;
  3116.   prev_insn = insn;
  3117. }
  3118.  
  3119. /* Store 1 in *WRITES_PTR for those categories of memory ref
  3120.    that must be invalidated when the expression WRITTEN is stored in.
  3121.    If WRITTEN is null, say everything must be invalidated.  */
  3122.  
  3123. static void
  3124. note_mem_written (written, writes_ptr)
  3125.      rtx written;
  3126.      struct write_data *writes_ptr;
  3127. {
  3128.   static struct write_data everything = {1, 1, 1};
  3129.  
  3130.   if (written == 0)
  3131.     *writes_ptr = everything;
  3132.   else if (GET_CODE (written) == MEM)
  3133.     {
  3134.       /* Pushing or popping the stack invalidates nothing.  */
  3135.       rtx addr = XEXP (written, 0);
  3136.       if ((GET_CODE (addr) == PRE_DEC || GET_CODE (addr) == PRE_INC
  3137.        || GET_CODE (addr) == POST_DEC || GET_CODE (addr) == POST_INC)
  3138.       && GET_CODE (XEXP (addr, 0)) == REG
  3139.       && REGNO (XEXP (addr, 0)) == STACK_POINTER_REGNUM)
  3140.     return;
  3141.       if (GET_MODE (written) == BLKmode)
  3142.     *writes_ptr = everything;
  3143.       else if (cse_rtx_addr_varies_p (written))
  3144.     {
  3145.       /* A varying address that is a sum indicates an array element,
  3146.          and that's just as good as a structure element
  3147.          in implying that we need not invalidate scalar variables.  */
  3148.       if (!(written->in_struct
  3149.         || GET_CODE (XEXP (written, 0)) == PLUS))
  3150.         writes_ptr->all = 1;
  3151.       writes_ptr->nonscalar = 1;
  3152.     }
  3153.       writes_ptr->var = 1;
  3154.     }
  3155. }
  3156.  
  3157. /* Perform invalidation on the basis of everything about an insn
  3158.    except for invalidating the actual places that are SET in it.
  3159.    This includes the places CLOBBERed, and anything that might
  3160.    alias with something that is SET or CLOBBERed.
  3161.  
  3162.    W points to the writes_memory for this insn, a struct write_data
  3163.    saying which kinds of memory references must be invalidated.
  3164.    X is the pattern of the insn.  */
  3165.  
  3166. static void
  3167. invalidate_from_clobbers (w, x)
  3168.      struct write_data *w;
  3169.      rtx x;
  3170. {
  3171.   /* If W->var is not set, W specifies no action.
  3172.      If W->all is set, this step gets all memory refs
  3173.      so they can be ignored in the rest of this function.  */
  3174.   if (w->var)
  3175.     invalidate_memory (w);
  3176.  
  3177.   if (GET_CODE (x) == CLOBBER)
  3178.     {
  3179.       rtx ref = XEXP (x, 0);
  3180.       if (ref
  3181.       && (GET_CODE (ref) == REG || GET_CODE (ref) == SUBREG
  3182.           || (GET_CODE (ref) == MEM && ! w->all)))
  3183.     invalidate (ref);
  3184.     }
  3185.   else if (GET_CODE (x) == PARALLEL)
  3186.     {
  3187.       register int i;
  3188.       for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
  3189.     {
  3190.       register rtx y = XVECEXP (x, 0, i);
  3191.       if (GET_CODE (y) == CLOBBER)
  3192.         {
  3193.           rtx ref = XEXP (y, 0);
  3194.           if (ref
  3195.           &&(GET_CODE (ref) == REG || GET_CODE (ref) == SUBREG
  3196.              || (GET_CODE (ref) == MEM && !w->all)))
  3197.         invalidate (ref);
  3198.         }
  3199.     }
  3200.     }
  3201. }
  3202.  
  3203. static void cse_basic_block ();
  3204.  
  3205. /* Perform cse on the instructions of a function.
  3206.    F is the first instruction.
  3207.    NREGS is one plus the highest pseudo-reg number used in the instruction.
  3208.  
  3209.    Returns 1 if jump_optimize should be redone due to simplifications
  3210.    in conditional jump instructions.  */
  3211.  
  3212. int
  3213. cse_main (f, nregs)
  3214.      /* f is the first instruction of a chain of insns for one function */
  3215.      rtx f;
  3216.      /* nregs is the total number of registers used in it */
  3217.      int nregs;
  3218. {
  3219.   register rtx insn = f;
  3220.   register int i;
  3221.  
  3222.   cse_jumps_altered = 0;
  3223.  
  3224.   init_recog ();
  3225.  
  3226.   max_reg = nregs;
  3227.  
  3228.   all_minus_one = (int *) alloca (nregs * sizeof (int));
  3229.   consec_ints = (int *) alloca (nregs * sizeof (int));
  3230.   for (i = 0; i < nregs; i++)
  3231.     {
  3232.       all_minus_one[i] = -1;
  3233.       consec_ints[i] = i;
  3234.     }
  3235.  
  3236.   reg_next_eqv = (int *) alloca (nregs * sizeof (int));
  3237.   reg_prev_eqv = (int *) alloca (nregs * sizeof (int));
  3238.   reg_qty = (int *) alloca (nregs * sizeof (int));
  3239.   reg_rtx = (rtx *) alloca (nregs * sizeof (rtx));
  3240.   reg_in_table = (int *) alloca (nregs * sizeof (int));
  3241.   reg_tick = (int *) alloca (nregs * sizeof (int));
  3242.  
  3243.   /* Discard all the free elements of the previous function
  3244.      since they are allocated in the temporarily obstack.  */
  3245.   bzero (table, sizeof table);
  3246.   free_element_chain = 0;
  3247.   n_elements_made = 0;
  3248.  
  3249.   /* Loop over basic blocks */
  3250.   while (insn)
  3251.     {
  3252.       register rtx p = insn;
  3253.       register int i = 0;
  3254.       register int last_uid;
  3255.  
  3256.       max_qty = 0;
  3257.  
  3258.       /* Find end of next basic block */
  3259.       while (p && GET_CODE (p) != CODE_LABEL)
  3260.     {
  3261.       /* Don't cse out the end of a loop.  This makes a difference
  3262.          only for the unusual loops that always execute at least once;
  3263.          all other loops have labels there so we will stop in any case.
  3264.          Cse'ing out the end of the loop is dangerous because it
  3265.          might cause an invariant expression inside the loop
  3266.          to be reused after the end of the loop.  This would make it
  3267.          hard to move the expression out of the loop in loop.c,
  3268.          especially if it is one of several equivalent expressions
  3269.          and loop.c would like to eliminate it.
  3270.          The occasional optimizations lost by this will all come back
  3271.          if loop and cse are made to work alternatingly.  */
  3272.       if (GET_CODE (p) == NOTE
  3273.           && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
  3274.         break;
  3275.  
  3276.       /* A PARALLEL can have lots of SETs in it,
  3277.          especially if it is really an ASM_OPERANDS.  */
  3278.       if (GET_CODE (p) == INSN && GET_CODE (PATTERN (p)) == PARALLEL)
  3279.         max_qty += XVECLEN (PATTERN (p), 0);
  3280.       else
  3281.         i++;
  3282.  
  3283.       last_uid = INSN_UID (p);
  3284.       p = NEXT_INSN (p);
  3285.     }
  3286.  
  3287.       cse_basic_block_end = last_uid;
  3288.       cse_basic_block_start = INSN_UID (insn);
  3289.  
  3290.       max_qty += max_reg + i * MAX_SETS_PER_INSN;
  3291.       /* BEGIN PATCH HERE */
  3292.       max_qty += i;
  3293.       /* END PATCH HERE */
  3294.  
  3295.       cse_basic_block (insn, p);
  3296.  
  3297.       insn = p ? NEXT_INSN (p) : 0;
  3298.     }
  3299.  
  3300.   /* Tell refers_to_mem_p that qty_const info is not available.  */
  3301.   qty_const = 0;
  3302.  
  3303.   if (max_elements_made < n_elements_made)
  3304.     max_elements_made = n_elements_made;
  3305.  
  3306.   return cse_jumps_altered;
  3307. }
  3308.  
  3309. static void
  3310. cse_basic_block (from, to)
  3311.      register rtx from, to;
  3312. {
  3313.   register rtx insn;
  3314.   int *qv1 = (int *) alloca (max_qty * sizeof (int));
  3315.   int *qv2 = (int *) alloca (max_qty * sizeof (int));
  3316.   rtx *qv3 = (rtx *) alloca (max_qty * sizeof (rtx));
  3317.  
  3318.   qty_first_reg = qv1;
  3319.   qty_last_reg = qv2;
  3320.   qty_const = qv3;
  3321.   qty_const_insn = (rtx *) alloca (max_qty * sizeof (rtx));
  3322.  
  3323.   new_basic_block ();
  3324.  
  3325.   for (insn = from; insn != to; insn = NEXT_INSN (insn))
  3326.     {
  3327.       register RTX_CODE code = GET_CODE (insn);
  3328.       if (code == INSN || code == JUMP_INSN || code == CALL_INSN)
  3329.     cse_insn (insn);
  3330.       /* Memory, and some registers, are invalidate by subroutine calls.  */
  3331.       if (code == CALL_INSN)
  3332.     {
  3333.       register int i;
  3334.       static struct write_data everything = {1, 1, 1};
  3335.       invalidate_memory (&everything);
  3336.       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  3337.         if (call_used_regs[i] && reg_rtx[i]
  3338.         && i != FRAME_POINTER_REGNUM
  3339.         && i != ARG_POINTER_REGNUM)
  3340.           invalidate (reg_rtx[i]);
  3341.     }
  3342.       /* Loop beginnings are often followed by jumps
  3343.      (that enter the loop above the endtest).
  3344.      See if we can prove the loop will be executed at least once;
  3345.      if so, delete the jump.  Also perhaps we can prove loop
  3346.      will never be executed and delete the entire thing.  */
  3347.       if (code == NOTE
  3348.       && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
  3349.       && GET_CODE (NEXT_INSN (insn)) == JUMP_INSN)
  3350.     {
  3351.       predecide_loop_entry (insn);
  3352.       /* Whether that jump was deleted or not,
  3353.          it certainly is the end of the basic block.
  3354.          Since the jump is unconditional,
  3355.          it requires no further processing here.  */
  3356.       break;
  3357.     }
  3358.     }
  3359.  
  3360.   if (next_qty > max_qty)
  3361.     abort ();
  3362. }
  3363.